답안 #381201

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381201 2021-03-24T18:02:41 Z VodkaInTheJar Homecoming (BOI18_homecoming) C++14
0 / 100
193 ms 262148 KB
#include <bits/stdc++.h>
#include "homecoming.h"

using namespace std;

const long long inf = 1e18;

long long solve(int n, int k, int *a, int *b)
{
	vector <vector <long long> > dp(n+1);
	for (int i = 1; i <= n; i++)
	dp[i].resize(n+1);
	
	vector <long long> pra(n+1, 0), prb(n+1, 0);
	for (int i = 1; i <= n; i++)
	{
		pra[i] = pra[i-1] + a[i-1];
		prb[i] = prb[i-1] + b[i-1];
	}
	
	for (int i = 1; i <= n; i++)
	{
		dp[i][i-1] = 0;
		long long curr_max = -inf;
		for (int j = i; j <= n; j++)
		{
			dp[i][j] = dp[i][j-1];
			if (j - k + 1 < i)
			continue;
			
			int pos = j - k + 1; 
			curr_max = max(curr_max, prb[pos-1] - pra[pos-1] + dp[i][pos-1]);
			
			dp[i][j] = max(dp[i][j], curr_max - prb[j] + pra[pos]);
		}
	}
	
	long long bb = 0;
	for (int i = 1; i <= n; i++)
	{
		bb -= b[i-1];
		bb += a[i-1];
	}
	
	long long ans = max(bb, dp[1][n]); 
	for (int i = 1; i <= n; i++)
	{
		for (int j = i; j <= n; j++)
	    {
		    long long curr = dp[i][j];
		    curr -= prb[i-1];
		    curr -= prb[n] - prb[j];
		    
		    if (i-1 >= k)
		    curr += pra[i-k];
		    
		    if (n-k+1 > j)
		    curr += pra[n-k+1] - pra[j];
		    
		    int l = max(j+1, n - k + 2), r = min(n, i+n-k);
		    if (l <= r)
		    curr += pra[r] - pra[l-1];
		    
		    ans = max(ans, curr);
		}
	}
	
	bool can = false;
	if (bb == ans || ans == 0)
	can = true;
	
	for (int i = 1; i <= n; i++)
	{
		for (int j = i; j <= n; j++)
	    {
		    long long curr = dp[i][j];
		    curr -= prb[i-1];
		    curr -= prb[n] - prb[j];
		    
		    if (i-1 >= k)
		    curr += pra[i-k];
		    
		    if (n-k+1 > j)
		    curr += pra[n-k+1] - pra[j];
		    
		    int l = max(j+1, n - k + 2), r = min(n, i+n-k);
		    if (l <= r)
		    curr += pra[r] - pra[l-1];
		    
		    if (j - i + 1 <= 2 * k && curr == ans)
		    can = true;
		}
	}
	
	assert(can);
	
	return ans;
}

/*
const int maxn = 1e3 + 3; 

int n, k;
int a[maxn], b[maxn];
int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	cin >> a[i] >> b[i];
	
	cout << solve(n, k, a, b) << endl; 
}
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 3 ms 2284 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 4 ms 2432 KB Output is correct
5 Runtime error 2 ms 808 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 3 ms 2284 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 4 ms 2432 KB Output is correct
5 Runtime error 2 ms 808 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 193 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 3 ms 2284 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 4 ms 2432 KB Output is correct
5 Runtime error 2 ms 808 KB Execution killed with signal 6