Cod sursa(job #1950530)

Utilizator cristina_borzaCristina Borza cristina_borza Data 3 aprilie 2017 09:24:42
Problema Semne Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <cstdlib>
#include <fstream>
#include <vector>
#include <ctime>

using namespace std;

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

const int Dim = 1e5;

vector <int> pls, mns;

int n, x, S, sum;
char ans[ Dim ];

int main() {
    srand (time (NULL));

    f >> n >> S;
    for (int i = 1; i <= n; ++i) {
        f >> x;
        int sgn = rand () % 2;

        if (sgn == 1) {
            pls.push_back (x);
            sum += x;
        }

        else {
            mns.push_back (x);
            sum -= x;
        }
    }

    while (sum != S) {
        if (sum < S) {
            int pos = rand () % mns.size();
            sum += 2 * mns[pos];

            pls.push_back (mns[pos]);

            swap (mns[pos], mns[mns.size() - 1]);
            mns.pop_back();
        }

        else {
            int pos = rand () % pls.size();
            sum -= 2 * pls[pos];

            mns.push_back (pls[pos]);

            swap (pls[pos], pls[pls.size() - 1]);
            pls.pop_back();
        }
    }

    for (int i = 1; i <= n; ++i)
        ans[i] = '-';

    for (vector <int> :: iterator it = pls.begin(); it != pls.end(); ++it) {
        ans[*it] = '+';
    }

    for (int i = 1; i <= n; ++i) {
        g << ans[i];
    }
    return 0;
}