Submission #962873

# Submission time Handle Problem Language Result Execution time Memory
962873 2024-04-14T09:14:08 Z happy_node Skyscraper (JOI16_skyscraper) C++17
100 / 100
567 ms 167880 KB
#include <bits/stdc++.h>
using namespace std;

const int mod=1e9+7;

int N,L;
int A[105];
int dp[105][105][2005][3];

// optimistic cost -> lower bound of the cost
// we keep increasing this optimistic cost as we go on
// we increase this as many times as the number of open borders of connected components
// # of components equal to leftmost or rightmost fixed

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);

	cin>>N>>L;
	for(int i=0;i<N;i++) cin>>A[i];

	if(N==1) {
		cout<<1<<'\n';
		return 0;
	}

	sort(A,A+N);

	// first lock or last lock
	dp[1][1][0][1]=2;

	// just middle 
	dp[1][1][0][0]=1;

	for(int i=1;i<N;i++) {
		int d=A[i]-A[i-1];
		for(int c=0;c<=N;c++) {
			for(int s=0;s<=L;s++) {
				for(int z=0;z<3;z++) {
					// merge 2 components
					if(c>0) {
						dp[i+1][c-1][s+d*(2*c-z)][z]+=1LL*dp[i][c][s][z]*(c-1)%mod;
						dp[i+1][c-1][s+d*(2*c-z)][z]%=mod;
					}

					// append to the beginning or end of one comp but don't lock
					dp[i+1][c][s+d*(2*c-z)][z]+=1ll*dp[i][c][s][z]*(2*c-z)%mod;
					dp[i+1][c][s+d*(2*c-z)][z]%=mod;

					// append to the beginning or end and lock
					dp[i+1][c][s+d*(2*c-z)][z+1]+=1ll*(2-z)*dp[i][c][s][z]%mod;
					dp[i+1][c][s+d*(2*c-z)][z+1]%=mod;

					// make new component

					// first or last component but not lock
					dp[i+1][c+1][s+d*(2*c-z)][z]+=1ll*(2-z)*dp[i][c][s][z]%mod;
					dp[i+1][c+1][s+d*(2*c-z)][z]%=mod;

					// first or last component and lock
					dp[i+1][c+1][s+d*(2*c-z)][z+1]+=1ll*(2-z)*dp[i][c][s][z]%mod;
					dp[i+1][c+1][s+d*(2*c-z)][z+1]%=mod;

					// make as middle component
					if(c>1) {
						dp[i+1][c+1][s+d*(2*c-z)][z]+=1ll*dp[i][c][s][z]*(c-1)%mod;
						dp[i+1][c+1][s+d*(2*c-z)][z]%=mod;
					}
				}
			}
		}
	}
		
	int ans=0;
	for(int s=0;s<=L;s++) {
		ans+=dp[N][1][s][2];
		ans%=mod;
	}
	cout<<ans<<'\n';

}

Compilation message

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:50:34: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
   50 |      dp[i+1][c][s+d*(2*c-z)][z+1]+=1ll*(2-z)*dp[i][c][s][z]%mod;
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:38:18: note: within this loop
   38 |     for(int z=0;z<3;z++) {
      |                 ~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 4 ms 3928 KB Output is correct
6 Correct 4 ms 3932 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 4 ms 3932 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 3420 KB Output is correct
8 Correct 2 ms 3420 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 4 ms 3928 KB Output is correct
6 Correct 4 ms 3932 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 4 ms 3932 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 2 ms 3420 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3420 KB Output is correct
17 Correct 2 ms 3420 KB Output is correct
18 Correct 2 ms 3420 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 2 ms 3676 KB Output is correct
21 Correct 10 ms 10076 KB Output is correct
22 Correct 278 ms 87328 KB Output is correct
23 Correct 534 ms 166036 KB Output is correct
24 Correct 425 ms 134648 KB Output is correct
25 Correct 549 ms 167632 KB Output is correct
26 Correct 467 ms 149060 KB Output is correct
27 Correct 161 ms 72788 KB Output is correct
28 Correct 178 ms 79264 KB Output is correct
29 Correct 324 ms 112208 KB Output is correct
30 Correct 567 ms 167880 KB Output is correct