Submission #386275

#TimeUsernameProblemLanguageResultExecution timeMemory
386275vanicBootfall (IZhO17_bootfall)C++14
44 / 100
1088 ms7628 KiB
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <array>
#include <bitset>
#include <cassert>

using namespace std;

const int maxn=505, Log=9, pot=(1<<Log);

vector < int > potencijal;

int sum;
bitset < maxn*maxn > dp[maxn];
int n;
int br;
int tren;
int p;

void dodaj(int x){
	for(int i=sum; i>=x; --i){
		dp[br][i]=dp[br-1][i-x]|dp[br-1][i];
	}
	for(int i=0; i<x; ++i){
		dp[br][i]=dp[br-1][i];
	}
	br++;
}


void probaj(){
	vector < int > novi;
	if(p==2){
		if((tren&1) || !dp[br-1][tren/2]){
			p=0;
		}
		else{
			p=1;
		}
//		cout << "hehe " << p <<  endl;
	}
	else if(p){
		for(int i=1; i<sum; ++i){
			if(!((i+tren)&1) && dp[br-1][(i+tren)/2]){
				potencijal.push_back(i);
			}
		}
		p=0;
	}
	else{
		for(int i=0; i<(int)potencijal.size(); i++){
			if(!((potencijal[i]+tren)&1) && dp[br-1][(potencijal[i]+tren)/2]){
				novi.push_back(potencijal[i]);
			}
		}
		potencijal=novi;
	}
}

struct tournament{
	vector < int > t[pot*2];
	void update(int L, int D, int x, int l, int d, int val){
		if(L>=l && D<=d){
			t[x].push_back(val);
			return;
		}
		if((L+D)/2>=l){
			update(L, (L+D)/2, x*2, l, d, val);
		}
		if((L+D)/2+1<=d){
			update((L+D)/2+1, D, x*2+1, l, d, val);
		}
	}
	void rokaj(int x){
		for(int i=0; i<(int)t[x].size(); i++){
			tren+=t[x][i];
			dodaj(t[x][i]);
		}
		if(x>=pot){
			if(p==2 || br==n){
//				cout << br << endl;
//				cout << "uso" << endl;
				probaj();
			}
			br-=t[x].size();
			for(int i=0; i<(int)t[x].size(); i++){
				tren-=t[x][i];
			}
			return;
		}
		rokaj(x*2);
		rokaj(x*2+1);
		br-=t[x].size();
		for(int i=0; i<(int)t[x].size(); i++){
			tren-=t[x][i];
		}
	}
};

int a[maxn];
tournament T;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> a[i];
		sum+=a[i];
		T.update(0, pot-1,  1, 0, i, a[i]);
		T.update(0, pot-1, 1, i+2, pot, a[i]);
	}
	dp[0][0]=1;
	br=1;
	p=2;
	T.rokaj(1);
	cout << potencijal.size() << '\n';
	for(int i=0; i<(int)potencijal.size(); i++){
		cout << potencijal[i] << ' ';
	}
	cout << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...