Pagini recente » Cod sursa (job #569982) | Cod sursa (job #1309572) | Cod sursa (job #2750448) | Cod sursa (job #204863) | Cod sursa (job #418784)
Cod sursa(job #418784)
#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;
nm=np=1;
fscanf(f1, "%d%lld", &N, &S);
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;
}