Submission #685058

# Submission time Handle Problem Language Result Execution time Memory
685058 2023-01-23T08:27:11 Z NeroZein Skyscraper (JOI16_skyscraper) C++14
100 / 100
250 ms 2732 KB
/*
 *    author: NeroZein
 *    created: 23.01.2023 11:07:10
*/
#include <bits/stdc++.h>
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int md = 1e9+7; 

inline void add(int& a, int b) {
	a += b;
	if(a >= md){
		a -= md;
	}
}

inline int mul(int a, int b) {
	return (int)((long long) a * b % md);
}

signed main(){

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	if(n == 1){
		cout << 1 << '\n';
		return 0; 
	}
	sort(a.begin(), a.end());
	vector<vector<array<int, 3>>>dp(n, vector<array<int, 3>>(m + 1));
	//ncc, sum, used endpoints
	dp[1][0][0] = 1; 
	dp[1][0][1] = 2;
	for (int i = 1; i < n; ++i) {
		vector<vector<array<int, 3>>>pd(n, vector<array<int, 3>>(m + 1));
		for (int j = 1; j < n; ++j) {
			for(int k = 0; k <= m; ++k) {
				for(int e = 0; e < 3; ++e){
					int diff = (2 * j - e) * (a[i] - a[i-1]); 
					if(k + diff > m){
						continue; 
					}
					//merge two cc
					if(j > 1) {
						add(pd[j - 1][k + diff][e], mul(j - 1, dp[j][k][e]));
					}
					//create a cc 
					if(j + 1 < n) {
						add(pd[j + 1][k + diff][e], mul(j + 1 - e, dp[j][k][e]));
					}
					//create a cc on the endpoints
					if(j + 1 < n && e < 2){
						add(pd[j + 1][k + diff][e + 1], mul(2 - e, dp[j][k][e]));
					}
					//add to the left or right to an already existing component but not become an endpoint
					add(pd[j][k + diff][e], mul(2 * j - e, dp[j][k][e]));
					//add to the left or right to an already existing component but become an endpoint
					if(e < 2){
						add(pd[j][k + diff][e + 1], mul(2 - e, dp[j][k][e]));
					}
				}
			}
		}
		swap(dp, pd);
	}
	int ans = 0;
	for(int i = 0; i <= m; ++i){
		add(ans, dp[1][i][2]);
	}
	cout << ans << '\n';

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 234 ms 1844 KB Output is correct
23 Correct 245 ms 2644 KB Output is correct
24 Correct 224 ms 2164 KB Output is correct
25 Correct 249 ms 2732 KB Output is correct
26 Correct 214 ms 2332 KB Output is correct
27 Correct 82 ms 1056 KB Output is correct
28 Correct 113 ms 1028 KB Output is correct
29 Correct 211 ms 1664 KB Output is correct
30 Correct 250 ms 2664 KB Output is correct