Cod sursa(job #1709257)

Utilizator CBOSTorinoUPB Andrei Bercaru CBOSTorino Data 28 mai 2016 11:30:00
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

struct info {
    int first;
    int last;
};

struct comparator {
    inline bool operator()(struct info a, struct info b) {
	return (a.last - a.first) < (b.last - b.first);
    }
}comp;

int main() {
    int T, N;
    int nr_posib = 0;
    std::vector<struct info> vect;

    std::ifstream f("consecutive.in");
    std::ofstream g("consecutive.out");

    f >> T >> N;

    for (int tests = 0; tests < T; tests++) {
        int sum = 0;
        int j;
        struct info current;

        for (int i = 1; i <= N / 2; i++) {
            sum = i;
            j = i + 1;

            while (sum < N) {
                sum += j;
                j++;
            }

            if (sum == N) {
                nr_posib++;
            current.first = i;
            current.last = j - 1;
            vect.push_back(current);
            }
        }

        std::sort(vect.begin(), vect.end(), comp);
        g << nr_posib << std::endl;
        for (int i = 0; i < vect.size(); i++) {
            g << vect[i].first << " " << vect[i].last << std::endl;
        }
    }

    f.close();
    g.close();

    return 0;
}