Pagini recente » Cod sursa (job #901552) | Cod sursa (job #2414493) | Cod sursa (job #807588) | Cod sursa (job #1345776) | Cod sursa (job #418797)
Cod sursa(job #418797)
#include <iostream>
#include <ctime>
using namespace std;
int N, A[50020], P[50020], M[50029], np, nm;
bool Sgn[50020];
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;
if(p>=nm) { p=nm-1; }
Sgn[A[M[p]]]=1;
sc+=2*A[M[p]];
P[np]=M[p]; np++;
M[p]=M[nm-1]; nm--;
}
else {
p=rand()%np+1;
if(p>=np) { p=np-1; }
Sgn[A[P[p]]]=0;
sc-=2*A[P[p]];
M[nm]=P[p]; nm++;
P[p]=P[np-1]; np--;
}
}
for(i=1; i<=N; i++) {
if(Sgn[i]) { fprintf(f2, "+"); continue; }
fprintf(f2, "-");
}
fclose(f1); fclose(f2);
return 0;
}