Pagini recente » Cod sursa (job #2806225) | Cod sursa (job #397114) | Cod sursa (job #987359) | Cod sursa (job #1554908) | Cod sursa (job #418788)
Cod sursa(job #418788)
#include <iostream>
#include <ctime>
using namespace std;
int N, A[50001], P[50001], M[50001], np, nm;
bool Sgn[50001];
long long S, sc;
int main() {
srand(time(NULL));
FILE *f1=fopen("semne.in", "r"), *f2=fopen("semne.out", "w");
int i, j, p, q;
fscanf(f1, "%d%lld", &N, &S);
nm=np=1;
for(i=1; i<=N; i++) {
fscanf(f1, "%d", &A[i]);
if(sc<=S) { sc+=A[i]; Sgn[i]=1; P[np]=i; np++; }
else { sc-=A[i]; Sgn[i]=0; M[nm]=i; nm++; }
}
while(sc!=S) {
if(sc<S) {
p=rand()%nm+1;
Sgn[A[M[p]]]=1;
sc+=2*A[M[p]];
P[np]=M[p]; np++;
M[p]=M[nm]; nm--;
}
else {
p=rand()%np+1;
Sgn[A[P[p]]]=0;
sc-=2*A[P[p]];
M[nm]=P[p]; nm++;
P[p]=P[np]; np--;
}
}
for(i=1; i<=N; i++) {
if(Sgn[i]) { fprintf(f2, "+"); continue; }
fprintf(f2, "-");
}
fclose(f1); fclose(f2);
return 0;
}