Submission #254651

# Submission time Handle Problem Language Result Execution time Memory
254651 2020-07-30T11:53:13 Z Saboon Homecoming (BOI18_homecoming) C++14
100 / 100
179 ms 79124 KB
#include<bits/stdc++.h>
#include"homecoming.h"
using namespace std;
typedef long long ll;
const int maxn = 2e6 + 20;
const ll inf = 1e18 + 7;

ll dp[maxn][2];
ll par[2*maxn];
int N;

ll getsum(int l, int r){
	if (l < 0)
		l += N, r += N;
	return par[r+1] - par[l];
}

ll solve(int n, int k, int* a, int* b){
	N = n;
	for (int i = 1; i <= n; i++)
		par[i] = par[i-1] + b[i-1];
	for (int i = n+1; i <= 2*n; i++)
		par[i] = par[i-1] + b[i-n-1];
	dp[0][0] = 0, dp[0][1] = -inf;
	for (int i = 1; i < n; i++){
		dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
		if (i-k+1 > 0)
			dp[i][1] = max(dp[i-k][0] + a[i-k+1] - getsum(i-k+1, i), dp[i-1][1] + a[i-k+1] - b[i]);
		else
			dp[i][1] = -inf;
	}
	ll answer = max(dp[n-1][0],dp[n-1][1]);
	ll tot = 0;
	for (int i = 0; i < n; i++)
		tot += a[i] - b[i];
	answer = max(answer, tot);
	for (int i = 1; i <= n; i++)
		par[i] = par[i-1] + a[i-1];
	for (int i = n+1; i <= 2*n; i++)
		par[i] = par[i-1] + a[i-n-1];
	dp[0][0] = -inf, dp[0][1] = 0;
	for (int i = 1; i < n; i++){
		dp[i][1] = max(dp[i-1][1], dp[i-1][0]);
		dp[i][0] = max(dp[i-1][0] + b[i] - a[i], dp[i-1][1] + b[i] - getsum(i-k+1, i));
	}
	answer = max(answer, tot + max(dp[n-1][0],dp[n-1][1]));
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 544 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 19968 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 179 ms 78664 KB Output is correct
4 Correct 3 ms 1152 KB Output is correct
5 Correct 9 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 544 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 43 ms 19968 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 179 ms 78664 KB Output is correct
14 Correct 3 ms 1152 KB Output is correct
15 Correct 9 ms 1368 KB Output is correct
16 Correct 177 ms 79124 KB Output is correct
17 Correct 76 ms 10364 KB Output is correct
18 Correct 143 ms 5376 KB Output is correct
19 Correct 128 ms 39764 KB Output is correct
20 Correct 133 ms 78840 KB Output is correct
21 Correct 125 ms 39884 KB Output is correct