summaryrefslogtreecommitdiff
path: root/Year_1/Programming_2/exercises/exam_20_07_20/ex1.cpp
blob: 4f08a74d3d9b158a6031a91f8cdde4d49b00d0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>

using namespace std;
int insertionsort(vector<int>& a, int n) {
    int c = 0;
    for(int i = 1; i < n; ++i) {
        int j = i-1;
        int key = a[i];
        while(j > -1 && a[j] > key) {
            swap(a[j+1], a[j]);
            --j;
            c++;
        }
        a[j+1] = key;
    }
    return c;
}
int main() {
    ifstream in("input.txt");
    ofstream out("output.txt");

    for(int ts = 0; ts < 100; ++ts) {
        int N;
        in >> N;
        int a, b;
        vector<int> v;
        for(int i = 0; i < N; ++i) {
            in >> a >> b;
            v.push_back(a+b);
        }
        out << insertionsort(v, v.size()) << endl;
    }
    in.close();
    out.close();
    return 0;
}