Submission #84893

# Submission time Handle Problem Language Result Execution time Memory
84893 2018-11-17T18:01:48 Z radoslav11 Skyscraper (JOI16_skyscraper) C++14
100 / 100
433 ms 170936 KB
/*
	We will use DP on connected componnents. The main idea is to add the elements one by one beginning from the one with the least value. 
	Also we will add the contribution of the difference between two consecutive values to the sum. The DP will have the following state: 
	dp[i][number of connected componnents (excluding corner componnents)][current sum][flag for left corner][flag for right corner].
	The complexity will be O(N * N * L).
*/


#include <bits/stdc++.h>
#define endl '\n'

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = 102;
const int MAXL = 1042;
const int mod = (int)1e9 + 7;

int n, l;
int a[MAXN];

void read()
{
	cin >> n >> l;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
}

int b[MAXN];
int dp[MAXN][MAXN][MAXL][2][2];

void add(int &x, int y)
{
	x += y;
	if(x >= mod) 
		x -= mod;
}

int mult(int x, int y) { return x * 1ll * y % mod; }

int rec(int i, int cnt, int S, int L, int R)
{
	S += L * b[i];
	S += R * b[i];
	S += cnt * 2 * b[i];

	if(S > l || cnt < 0) 
		return 0;

	if(i == n - 1) /// Last position
		return cnt == 0;

	int &memo = dp[i][cnt][S][L][R];
	if(memo != -1) 
		return memo;

	memo = 0;

	add(memo, mult(cnt * (cnt - 1), rec(i + 1, cnt - 1, S, L, R)));  /// merge two comps 
	
	add(memo, mult(cnt, rec(i + 1, cnt - 1, S, 1, R)));  /// add to L corner 
	add(memo, mult(cnt, rec(i + 1, cnt - 1, S, L, 1)));  /// add to R corner 
	add(memo, rec(i + 1, cnt, S, 1, R));  /// new comp to L corner
	add(memo, rec(i + 1, cnt, S, L, 1));  /// new comp to R corner
	
	add(memo, rec(i + 1, cnt + 1, S, L, R));  /// new comp
	add(memo, mult(2 * cnt, rec(i + 1, cnt, S, L, R)));  /// extend existing comp

	return memo;
}

void solve()
{
	memset(dp, -1, sizeof(dp));
	sort(a + 1, a + n + 1);
	for(int i = 0; i < n; i++)
		b[i] = i ? (a[i + 1] - a[i]) : 0;

	cout << rec(0, 0, 0, 0, 0) << endl;
}

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

	read();
	solve();
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 139 ms 170188 KB Output is correct
2 Correct 142 ms 170236 KB Output is correct
3 Correct 138 ms 170348 KB Output is correct
4 Correct 136 ms 170452 KB Output is correct
5 Correct 136 ms 170548 KB Output is correct
6 Correct 136 ms 170628 KB Output is correct
7 Correct 137 ms 170732 KB Output is correct
8 Correct 135 ms 170732 KB Output is correct
9 Correct 140 ms 170732 KB Output is correct
10 Correct 140 ms 170732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 170732 KB Output is correct
2 Correct 139 ms 170732 KB Output is correct
3 Correct 137 ms 170732 KB Output is correct
4 Correct 136 ms 170744 KB Output is correct
5 Correct 137 ms 170764 KB Output is correct
6 Correct 141 ms 170764 KB Output is correct
7 Correct 142 ms 170764 KB Output is correct
8 Correct 138 ms 170780 KB Output is correct
9 Correct 138 ms 170780 KB Output is correct
10 Correct 138 ms 170788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 170188 KB Output is correct
2 Correct 142 ms 170236 KB Output is correct
3 Correct 138 ms 170348 KB Output is correct
4 Correct 136 ms 170452 KB Output is correct
5 Correct 136 ms 170548 KB Output is correct
6 Correct 136 ms 170628 KB Output is correct
7 Correct 137 ms 170732 KB Output is correct
8 Correct 135 ms 170732 KB Output is correct
9 Correct 140 ms 170732 KB Output is correct
10 Correct 140 ms 170732 KB Output is correct
11 Correct 138 ms 170732 KB Output is correct
12 Correct 139 ms 170732 KB Output is correct
13 Correct 137 ms 170732 KB Output is correct
14 Correct 136 ms 170744 KB Output is correct
15 Correct 137 ms 170764 KB Output is correct
16 Correct 141 ms 170764 KB Output is correct
17 Correct 142 ms 170764 KB Output is correct
18 Correct 138 ms 170780 KB Output is correct
19 Correct 138 ms 170780 KB Output is correct
20 Correct 138 ms 170788 KB Output is correct
21 Correct 138 ms 170792 KB Output is correct
22 Correct 433 ms 170796 KB Output is correct
23 Correct 301 ms 170800 KB Output is correct
24 Correct 315 ms 170936 KB Output is correct
25 Correct 299 ms 170936 KB Output is correct
26 Correct 269 ms 170936 KB Output is correct
27 Correct 195 ms 170936 KB Output is correct
28 Correct 217 ms 170936 KB Output is correct
29 Correct 332 ms 170936 KB Output is correct
30 Correct 334 ms 170936 KB Output is correct