CSES - Putka Open 2015 – finaali - Results
Submission details
Task:Omenat
Sender:
Submission time:2015-12-20 16:05:15 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#132.7
Test results
testverdicttimescore
#1ACCEPTED0.07 s6.1details
#20.17 s0details
#3ACCEPTED0.07 s5.5details
#40.17 s0details
#5ACCEPTED0.06 s4.6details
#6ACCEPTED0.05 s5.7details
#70.16 s0details
#80.17 s0details
#9ACCEPTED0.06 s5details
#10ACCEPTED0.07 s5.8details

Code

#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MN=101;
typedef long long ll;
typedef pair<int,int> P;
P ps[MN];
int vs[MN];
const int MS = 100*1000;
const int MS2 = MS/2;
ll dp[MN][MS];
bool res[MN];
bool rres[MN];
int n;
bool cur[MN];
double rnd() {
return rand() / (double)RAND_MAX;
}
ll better(ll a, ll b) {
return llabs(a) < llabs(b) ? a : b;
}
int scount;
int ssum;
void solveSmall(ll bsum) {
if (llabs(bsum) > scount) {
for(int i=0; i<=scount; ++i) {
res[i] = bsum<0;
}
return;
}
int x=scount;
int sum=bsum;
while(x>0) {
int v=vs[x];
ll a = dp[x][sum+v + MS2];
ll b = dp[x][sum-v + MS2];
if (better(a,b) == a) {
res[x] = 1;
sum += v;
} else {
res[x] = 0;
sum -= v;
}
--x;
}
}
void sa() {
ll r=0;
for(int i=0; i<n; ++i) {
cur[i] = rand()&1;
r += (2*cur[i]-1)*vs[i];
}
ll best = r;
memcpy(res, cur, sizeof(res));
for(double t=10; t>0.01; t*=0.9999) {
int x = rand()%n;
int sign = cur[x] ? -2 : 2;
ll rr = r + sign * vs[x];
if (llabs(rr) < llabs(r) || t > rnd()) {
cur[x] ^= 1;
r = rr;
if (llabs(r) < llabs(best)) {
best = r;
memcpy(res,cur,sizeof(res));
}
}
}
}
int main() {
srand(666);
cin>>n;
for(int i=0; i<n; ++i) {
int x;
cin>>x;
ps[i]=P(x,i);
}
sort(ps,ps+n);
for(int i=0; i<n; ++i) vs[i]=ps[i].first;
int sum=0;
for(int i=0; i<n; ++i) {
sum+=vs[i];
if (sum < MS2) {
ssum = sum;
scount = i+1;
}
}
// cout<<"scount "<<scount<<'\n';
for(int i=0; i<MS; ++i) {
dp[0][i] = i-MS2;
}
for(int i=1; i<=scount; ++i) {
int v = vs[i-1];
for(int j=0; j<MS; ++j) {
if (j>v && j+v<MS) {
dp[i][j] = better(dp[i-1][j-v], dp[i-1][j+v]);
} else {
dp[i][j] = 1e15;
}
// if (llabs(dp[i][j]) < ssum) cout<<"dp "<<i<<' '<<j<<" = "<<dp[i][j]<<'\n';
}
}
// cout<<dp[scount][MS2]<<'\n';
// assert(sum<MS2);
if (scount == n) {
solveSmall(0);
} else {
sa();
}
for(int i=0; i<n; ++i) rres[ps[i].second] = res[i];
for(int i=0; i<n; ++i) cout<<1+rres[i]<<' ';cout<<'\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
95
779724552 231968220 985023789 ...

correct output
(empty)

user output
1 1 2 2 1 2 1 2 2 2 1 1 1 1 2 ...

Test 2

Verdict:

input
85
229722261 51722691 862338862 8...

correct output
(empty)

user output
(empty)

Test 3

Verdict: ACCEPTED

input
97
398995377 989444445 634573915 ...

correct output
(empty)

user output
1 1 1 1 1 2 2 2 2 1 2 2 2 1 2 ...

Test 4

Verdict:

input
99
843687873 164010938 51269970 4...

correct output
(empty)

user output
(empty)

Test 5

Verdict: ACCEPTED

input
90
864611617 418460939 773297829 ...

correct output
(empty)

user output
2 2 1 2 2 2 1 1 1 1 1 2 1 1 2 ...

Test 6

Verdict: ACCEPTED

input
92
289890246 25801423 763027596 7...

correct output
(empty)

user output
2 2 1 1 1 2 2 1 2 2 2 2 1 2 1 ...

Test 7

Verdict:

input
89
879039800 50522278 850785072 4...

correct output
(empty)

user output
(empty)

Test 8

Verdict:

input
96
27192469 222283781 681532515 1...

correct output
(empty)

user output
(empty)

Test 9

Verdict: ACCEPTED

input
100
186459081 254674429 394007236 ...

correct output
(empty)

user output
2 2 2 2 2 2 1 2 2 1 2 1 1 2 1 ...

Test 10

Verdict: ACCEPTED

input
98
612168861 979831717 671087051 ...

correct output
(empty)

user output
2 1 1 1 1 1 2 1 1 1 2 2 1 1 1 ...