Pagini recente » Cod sursa (job #255973) | Cod sursa (job #701235) | Cod sursa (job #96797) | Cod sursa (job #2339208) | Cod sursa (job #785903)
Cod sursa(job #785903)
#include <cstdio>
#include <cstdlib>
#include <ctime>
#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) {
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;
-- vfplus;
++ vfmin;
stmin [vfmin] = pozc;
}
else if (Sc < S) {
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;
-- vfmin;
++ vfplus;
stplus [vfplus] = 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");
}