Submission #308906

#TimeUsernameProblemLanguageResultExecution timeMemory
308906arnold518Skyscraper (JOI16_skyscraper)C++14
20 / 100
2090 ms118656 KiB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int MAXN = 100;
const ll MOD = 1e9+7;
 
int N, L, A[MAXN+10], S[MAXN+10];
 
//ll dp[110][110][5000][4];
//map<tuple<int, int, int, int>, int> dp;
unordered_map<ll, ll> dp;
 
int v1, v2;
 
ll solve(int pos, int comp, int val, int state)
{
	if(val>L) return 0;
	if(val+S[pos]*2<0) return 0;
	v1=min(v1, val); v2=max(v2, val);
	if(pos==N+1)
	{
		if(comp==1 && 0<=val && state==3) return 1;
		else return 0;
	}
	ll t=val; t*=101; t+=pos; t*=101; t+=comp; t*=4; t+=state;
	if(dp.find(t)!=dp.end()) return dp[t];
	ll ret=0;
 
	ret=0;
	int sz=__builtin_popcount(state);
 
	ret+=(comp+1-sz)*solve(pos+1, comp+1, val-2*A[pos], state);
	ret+=(comp*2-sz)*solve(pos+1, comp, val, state);
	if(comp) ret+=(comp-1)*solve(pos+1, comp-1, val+2*A[pos], state);
	if((state&1)==0) ret+=solve(pos+1, comp, val+A[pos], state|1);
	if((state&1)==0) ret+=solve(pos+1, comp+1, val-A[pos], state|1);
	if((state&2)==0) ret+=solve(pos+1, comp, val+A[pos], state|2);
	if((state&2)==0) ret+=solve(pos+1, comp+1, val-A[pos], state|2);
	//printf("%d %d %d %d : %lld\n", pos, comp, val, state, ret);
 
	ret%=MOD;
	dp[t]=ret;
	return ret;
}
 
int main()
{
	scanf("%d%d", &N, &L);
	if(N==1) return !printf("1");
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);
	sort(A+1, A+N+1);
	for(int i=2; i<=N; i++) A[i]-=A[1]; A[1]=0;
	for(int i=1; i<=N; i++) S[i]=A[i];
	for(int i=N; i>=1; i--) S[i]+=S[i+1];
 
	//memset(dp, -1, sizeof(dp));
	printf("%lld\n", solve(1, 0, 0, 0));
	//printf("!%d %d\n", v1, v2);
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:56:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   56 |  for(int i=2; i<=N; i++) A[i]-=A[1]; A[1]=0;
      |  ^~~
skyscraper.cpp:56:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   56 |  for(int i=2; i<=N; i++) A[i]-=A[1]; A[1]=0;
      |                                      ^
skyscraper.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |  scanf("%d%d", &N, &L);
      |  ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:54:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...