Pagini recente » Cod sursa (job #875323) | Cod sursa (job #266981) | Cod sursa (job #967198) | Cod sursa (job #633932) | Cod sursa (job #785913)
Cod sursa(job #785913)
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
#define DM 50005
int v [DM], stmin [DM], stplus [DM];
bool answ [DM];
char afis [] = {'-', '+'};
int main (){
freopen ("semne.in", "r", stdin);
freopen ("semne.out", "w", stdout);
int n, i, pozc, vfmin = 0, vfplus = 0;
long long S, Sc = 0;
bool danu;
srand (time (NULL));
scanf ("%d%lld", &n, &S);
for (i = 0; i < n; ++ i) {
scanf ("%d", &v [i]);
danu = rand () % 2;
if (!danu) {
Sc -= v [i];
answ [i] = 0;
++ vfmin;
stmin [vfmin] = i;
}
else {
Sc += v [i];
answ [i] = 1;
++ vfplus;
stplus [vfplus] = i;
}
}
while (Sc != S) {
if (Sc > S && vfplus) {
pozc = rand () % vfplus + 1;
Sc -= 2 * v [stplus [pozc]];
//fprintf (stderr, "Sc = %lld, pozc = %d, stplus [pozc] = %d, v [stplus [pozc]] = %d\n", Sc, pozc, stplus [pozc], v [stplus [pozc]]);
answ [stplus [pozc]] = 0;
swap (stplus [pozc], stplus [vfplus]);
-- vfplus;
++ vfmin;
stmin [vfmin] = v [stplus [pozc]];
}
else if (Sc < S && vfmin) {
pozc = rand () % vfmin + 1;
Sc += 2 * v [stmin [pozc]];
//fprintf (stderr, "Sc = %lld, pozc = %d, stplus [pozc] = %d, v [stplus [pozc]] = %d\n", Sc, pozc, stplus [pozc], v [stplus [pozc]]);
answ [stmin [pozc]] = 1;
swap (stmin [pozc], stmin [vfmin]);
-- vfmin;
++ vfplus;
stplus [vfplus] = v [stmin [pozc]];
}
/*for (i = 0; i < n; ++ i)
fprintf (stderr, "%c", afis [answ [i]]);
fprintf (stderr, "Sc = %I64d\n", Sc);*/
}
for (i = 0; i < n; ++ i)
printf ("%c", afis [answ [i]]);
printf ("\n");
}