Submission #282428

# Submission time Handle Problem Language Result Execution time Memory
282428 2020-08-24T12:21:34 Z Atalasion Homecoming (BOI18_homecoming) C++14
100 / 100
217 ms 157304 KB
#include <bits/stdc++.h>
#include "homecoming.h"
 
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
const int NN = 4000000 + 10;
const int MOD = 1000000007;
const int LOG = 20;
const ll INF = 1000000000000000010;
const int delta = 11353;
 
ll dp[NN][2], sm[NN], ps[NN];
int n, k, A[NN], B[NN];
 
ll solve(int N, int K, int a[], int b[]){
	ps[0] = 0;
	n = N;
	for (int i = 0; i < n; i++) A[i] = a[i], B[i] = b[i];
	for (int i = n; i < 2 * n; i++) B[i] = B[i - n];
	for (int i = 1; i <= 2 * n; i++){
		ps[i] = ps[i - 1] + B[i - 1];
	//	cout << i << ' ' << ps[i] << '\n';
	}
	if (K == n){
		ll ans = 0;
		ll Sum = 0;
		for (int i = 0; i < n; i++) Sum += A[i], Sum -= B[i];
		return max(ans, Sum);
	}
	for (int i = 0; i < n; i++)sm[i] = ps[i + 1 + K - 1] - ps[i];
//	for (int i = 0; i < n; i++) cout << i << ' ' << sm[i] << '\n';
	ll ans = 0;
	ll Sum = 0;
	for (int i = 0; i < n; i++) Sum += A[i], Sum -= B[i];
	ans = max(ans, Sum);
	for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) dp[i][j] = -INF;
	dp[n - 1][0] = 0, dp[n - 1][1] = A[n - 1] - B[n - 1];
	//cout << dp[n - 1][0] << ' ' << dp[n - 1][1] << '\n';
	for (int i = n - 2; i >= 0; i--){
		dp[i][0] = max(dp[i + 1][1], dp[i + 1][0]);
		dp[i][1] = max(dp[i + 1][0] + A[i] - sm[i], dp[i + 1][1] + A[i] - B[i]);
		//cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
	}
	Sum = A[0] - sm[0];
	for (int i = 1; i < n; i++){
		ans = max(ans, dp[i][0] + Sum);
		Sum += sm[i - 1] - B[i - 1];
		Sum += A[i] - sm[i];
	}
	//cout << ans << '\n';
	for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) dp[i][j] = -INF;
	dp[n - 1][0] = 0, dp[n - 1][1] = A[n - 1] - sm[n - 1];
	for (int i = n - 2; i >= 0; i--){
		dp[i][0] = max(dp[i + 1][0], dp[i + 1][1]);
		dp[i][1] = max(dp[i + 1][0] + A[i] - sm[i], dp[i + 1][1] + A[i] - B[i]);
		//cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
	}
	ans = max(ans, dp[0][0]);
	return ans;
}
# 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 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 744 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 30120 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 203 ms 157304 KB Output is correct
4 Correct 2 ms 1792 KB Output is correct
5 Correct 11 ms 3800 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 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 744 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 52 ms 30120 KB Output is correct
12 Correct 3 ms 1024 KB Output is correct
13 Correct 203 ms 157304 KB Output is correct
14 Correct 2 ms 1792 KB Output is correct
15 Correct 11 ms 3800 KB Output is correct
16 Correct 217 ms 157160 KB Output is correct
17 Correct 83 ms 32896 KB Output is correct
18 Correct 153 ms 43960 KB Output is correct
19 Correct 146 ms 86376 KB Output is correct
20 Correct 164 ms 140312 KB Output is correct
21 Correct 144 ms 83912 KB Output is correct