Submission #91184

# Submission time Handle Problem Language Result Execution time Memory
91184 2018-12-26T13:20:44 Z emil_physmath Bootfall (IZhO17_bootfall) C++14
6 / 100
1000 ms 123032 KB
#include <iostream>
#include <stdio.h>
#include <unordered_set>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
const int MAXN=500, MAXSUM=500*500;

int a[MAXN+1];
bool dp[MAXN][MAXSUM+1];

void AllPossSums(int arr[], int n, vector<int> &);
int main()
{
	int n, sum=0;
	cin>>n;
	set<int> mods;
	for (int i=0; i<n; i++)
	{
		scanf("%d", a+i);
		mods.insert(a[i]%2);
		sum+=a[i];
	}
	if (mods.size()>1)
	{
		cout<<"0\n";
		return 0;
	}
	if (*(mods.begin()))
		if (n%2)
		{
			cout<<"0\n";
			return 0;
		}
	vector<int> curSums;
	AllPossSums(a, n, curSums);
	bool f=false;
	for (int i=0; i<curSums.size(); i++)
		if (curSums[i]*2==sum)
		{
			f=true;
			break;
		}
	if (!f)
	{
		cout<<"0\n";
		return 0;
	}
	unordered_set<int> ans, curAns;
	for (int i=0; i<n; i++)
	{ // #i doesn't play.
		sum-=a[i];
		swap(a[i], a[n-1]);
		AllPossSums(a, n-1, curSums);
		curAns.clear();
		for (int j=0; j<curSums.size(); j++)
			curAns.insert(abs(sum-curSums[j]-curSums[j]));
		if (!i)
		{
			ans=curAns;
			swap(a[i], a[n-1]);
			sum+=a[i];
			continue;
		}
		unordered_set<int> temp;
		if (curAns.size()<ans.size())
		{
			for (auto it=curAns.begin(); it!=curAns.end(); it++)
				if (ans.find(*it)!=ans.end())
					temp.insert(*it);
		}
		else
			for (auto it=ans.begin(); it!=ans.end(); it++)
				if (curAns.find(*it)!=curAns.end())
					temp.insert(*it);
		ans=temp;
		swap(a[i], a[n-1]);
		sum+=a[i];
	}
	cout<<ans.size()<<'\n';
	set<int> ansOut(ans.begin(), ans.end());
	for (auto it=ansOut.begin(); it!=ansOut.end(); it++)
		cout<<*it<<' ';
	cout<<endl;

	char I;
	cin >> I;
	return 0;
}

void AllPossSums(int arr[], int n, vector<int> & res)
{
	int sum=0;
	for (int i=0; i<n; i++)
		sum+=arr[i];
	memset(dp, 0, sizeof(dp));
	for (int i=0; i<=n; i++)
		dp[i][0]=true;
	for (int i=1; i<=n; i++)
	{
		dp[i][arr[i-1]]=true;
		for (int j=1; j<=sum; j++)
		{
			if (dp[i-1][j]==true)
			{
				dp[i][j]=true;
				dp[i][j+arr[i-1]]=true;
			}
		}
	}
	res.clear();
	for (int j=0; j<=sum; j++)
		if (dp[n][j]==true)
			res.push_back(j);
}

Compilation message

bootfall.cpp: In function 'int main()':
bootfall.cpp:40:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<curSums.size(); i++)
                ~^~~~~~~~~~~~~~~
bootfall.cpp:58:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j=0; j<curSums.size(); j++)
                 ~^~~~~~~~~~~~~~~
bootfall.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
10 Execution timed out 1029 ms 123032 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
10 Execution timed out 1029 ms 123032 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
10 Execution timed out 1029 ms 123032 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
10 Execution timed out 1029 ms 123032 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 221 ms 122716 KB Output is correct
2 Correct 283 ms 122716 KB Output is correct
3 Correct 97 ms 122796 KB Output is correct
4 Correct 224 ms 122852 KB Output is correct
5 Correct 473 ms 123032 KB Output is correct
6 Correct 348 ms 123032 KB Output is correct
7 Correct 284 ms 123032 KB Output is correct
8 Correct 477 ms 123032 KB Output is correct
9 Correct 2 ms 123032 KB Output is correct
10 Execution timed out 1029 ms 123032 KB Time limit exceeded
11 Halted 0 ms 0 KB -