Cod sursa(job #999215)

Utilizator harababurelPuscas Sergiu harababurel Data 19 septembrie 2013 18:04:18
Problema Congr Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>
#define nmax 600005
using namespace std;


int v[nmax], used[nmax], unused[nmax];
int p, x, y, aux, s = 0;


int main() {
    srand((unsigned)time(0));
    ifstream f("congr.in");
    ofstream g("congr.out");

    f>>p;
    for(int i=1; i<2*p; i++) {
        f>>v[i];
        v[i] %= p;
    }
    used[0] = 0;
    unused[0] = 0;

    for(int i=1; i<=p; i++) {
        used[++used[0]] = i;
        s += v[i];
    }
    for(int i=p+1; i<2*p; i++)
        unused[++unused[0]] = i;


    while(1) {
        if(s == 0) break;
        if(s >= p && s % p == 0) break;

        x = rand() % used[0] + 1;
        y = rand() % unused[0] + 1;

        s = s - v[used[x]] + v[unused[y]];

        aux = used[x];
        used[x] = unused[y];
        unused[y] = aux;
    }

    for(int i=1; i<=used[0]; i++)
        g<<used[i]<<" ";
    g<<"\n";

    return 0;
}