Cod sursa(job #732748)

Utilizator vendettaSalajan Razvan vendetta Data 10 aprilie 2012 21:34:22
Problema Semne Scor 20
Compilator cpp Status done
Runda Lista lui wefgef Marime 0.9 kb
#include <fstream>
#include <cstdlib>
#include <ctime>
#define nmax 50005

using namespace std;

ifstream f("semne.in");
ofstream g("semne.out");

int n, s, cnt, a[nmax];
string rez;


void citeste(){

    f >> n >> s;
    for(int i=0; i<n; i++) f >> a[i];

}

void rezolva(){

    srand(time(NULL));

    int ok = 1;
    int viz[nmax];
    while(ok){
        int nr = rand()%n + 1;
        rez.clear();
        int s2 = 0;
        while(cnt != nr){
            int x = rand() % n;
            if (viz[x] == 0){
                viz[x] = 1;
                ++cnt;
            }
        }
        for(int i=0; i<n; i++){
            if (viz[i] == 1) s2+=a[i], rez+='+', viz[i]=0;
            else s2-=a[i], rez+='-';
        }
        if (s2 == s){
            g << rez << "\n";
            ok = 0;
        }
        cnt = 0;
    }

}

int main(){

    citeste();
    rezolva();

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

    return 0;

}