CSES - Putka Open 2015 – finaali - Results
Submission details
Task:Omenat
Sender:
Submission time:2015-12-20 17:11:19 +0200
Language:C++
Status:READY
Result:78
Feedback
groupverdictscore
#1ACCEPTED77.5
Test results
testverdicttimescore
#1ACCEPTED0.76 s8.1details
#2ACCEPTED0.76 s7.6details
#3ACCEPTED0.76 s7.3details
#4ACCEPTED0.76 s8.6details
#5ACCEPTED0.77 s8.3details
#6ACCEPTED0.76 s7.2details
#7ACCEPTED0.76 s7details
#8ACCEPTED0.76 s7.5details
#9ACCEPTED0.77 s8.4details
#10ACCEPTED0.76 s7.5details

Code

#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <random>
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];

std::mt19937 rng(666);
double rnd() {
	return rng() / (double)rng.max();
}
ll better(ll a, ll b) {
	return llabs(a) < llabs(b) ? a : b;
}

int scount;
ll ssum;

void solveSmall(ll bsum) {
	if (llabs(bsum) > ssum) {
		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-1];
		ll a = dp[x-1][sum+v + MS2];
//		ll b = dp[x-1][sum-v + MS2];
//		if (better(a,b) == a) {
		if (a == dp[x][sum + MS2]) {
			res[x-1] = 1;
			sum += v;
		} else {
			res[x-1] = 0;
			sum -= v;
		}
		--x;
	}
}

ll smallRes(ll bsum) {
	if (llabs(bsum) > ssum) {
		return llabs(bsum) - ssum;
	}
	return llabs(dp[scount][MS2+bsum]);
}

ll ival2(ll r, P p) {
	int i=p.first;
	int j=p.second;
	int sign = cur[i] ? -2 : 2;
	int sign2 = cur[j] ? -2 : 2;
	return r + sign * vs[i] + sign2 * vs[j];
}
ll ival(ll r, int i) {
	int sign = cur[i] ? -2 : 2;
	return r + sign * vs[i];
}

P improve2(ll r) {
	P best=P(-1,-1);
	ll bv=r;
	for(int i=0; i<n; ++i) {
		for(int j=i+1; j<n; ++j) {
			P p(i,j);
			ll rr = ival2(r, p);
			if (llabs(rr) < llabs(bv)) {
				best = P(i,j);
				bv = rr;
			}
		}
	}
	return best;
}
int improve1(ll r) {
	int best=-1;
	ll bv=r;
	for(int i=0; i<n; ++i) {
		ll rr = ival(r, i);
		if (llabs(rr) < llabs(bv)) {
			best = i;
			bv = rr;
		}
	}
	return best;
}

void sa() {
	ll r=0;
	for(int i=scount; i<n; ++i) {
		cur[i] = rng()&1;
		r += (2*cur[i]-1)*vs[i];
	}
	ll best = smallRes(r);
	memcpy(res, cur, sizeof(res));
//	solveSmall(r);
	int iters=0;
	for(double t=20; t>0.001; t*=0.9999997) {
		++iters;
		int x = rng()%n;
		int sign = cur[x] ? -2 : 2;
		ll rr = r + sign * vs[x];
		ll srr = smallRes(rr);
//		if (srr < smallRes(r) || t>rnd()) {
		if (true) {
			cur[x] ^= 1;
			r = rr;
			if (srr < best) {
				best = srr;
				memcpy(res,cur,sizeof(res));
//				solveSmall(rr);
			}
		}
	}
	cerr<<"iters "<<iters<<'\n';
	cerr<<"best "<<best<<'\n';
}

ll bval=1e15;

void lol() {
	ll r=0;
	for(int i=0; i<n; ++i) {
//		cur[i] = rng()&1;
		cur[i] = res[i];
		r += (2*cur[i]-1)*vs[i];
	}
//	ll best = llabs(r);
//	memcpy(res, cur, sizeof(res));

	while(1) {
		ll x = improve1(r);
		P y = improve2(r);
		ll a = ival(r, x);
		ll b = ival2(r, y);
		if (llabs(a) >= llabs(r) && llabs(b) >= llabs(r)) break;
		if (llabs(a) < llabs(b)) {
			cur[x] ^= 1;
		} else {
			cur[y.first] ^= 1;
			cur[y.second] ^= 1;
		}
	}
	if (llabs(r) < llabs(bval)) {
		memcpy(res, cur, sizeof(res));
		bval = r;
	}
}

int main() {
	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;

	ll sum=0;
	for(int i=0; i<n; ++i) {
		sum+=vs[i];
		if (sum < MS2) {
			ssum = sum;
			scount = i+1;
		}
	}
	cerr<<"scount "<<scount<<'\n';
	scount=ssum=0;

	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 if (1) {
		sa();
		lol();
	} else {
		for(int i=0; i<1000; ++i) {
			lol();
		}
		cerr<<"res "<<bval<<'\n';
	}

	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
2 1 2 1 2 1 1 1 2 2 1 2 2 1 1 ...

Error:
scount 0
iters 33011621
best 72

Test 2

Verdict: ACCEPTED

input
85
229722261 51722691 862338862 8...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 218

Test 3

Verdict: ACCEPTED

input
97
398995377 989444445 634573915 ...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 410

Test 4

Verdict: ACCEPTED

input
99
843687873 164010938 51269970 4...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 20

Test 5

Verdict: ACCEPTED

input
90
864611617 418460939 773297829 ...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 42

Test 6

Verdict: ACCEPTED

input
92
289890246 25801423 763027596 7...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 508

Test 7

Verdict: ACCEPTED

input
89
879039800 50522278 850785072 4...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 992

Test 8

Verdict: ACCEPTED

input
96
27192469 222283781 681532515 1...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 264

Test 9

Verdict: ACCEPTED

input
100
186459081 254674429 394007236 ...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 34

Test 10

Verdict: ACCEPTED

input
98
612168861 979831717 671087051 ...

correct output
(empty)

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

Error:
scount 0
iters 33011621
best 298