其他分享
首页 > 其他分享> > Codeforces Round #638 (Div. 2) E—Phoenix and Berries dp

Codeforces Round #638 (Div. 2) E—Phoenix and Berries dp

作者:互联网

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 505;
int n, k;
int a[N], b[N];
bool f[N][N];
int main()
{ 
	scanf("%d%d", &n, &k);
	long long sum_a = 0, sum_b = 0;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d%d", &a[i], &b[i]);
		sum_a += a[i];
		sum_b += b[i];
	}
	//我们先按照颜色分,也就是 suma % k  +  sumb%k
	//suma % k <k     ,  sumb % k <k
	//也就是说,剩下的果子 是 小于2k  的 
	//也就是说 最多再能凑一个k出来
	//然后我们就枚举,可以把这些果子都集中在哪个树上,然后让他们再凑一个k出来  
	f[0][0] = true;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 0; j < k; j++)
		{
			f[i][j] = f[i - 1][j];
			for(int x = 0; x <= k; x++)
			{
				if(x <= a[i] && (k - x) <= b[i])
				{
					f[i][j] = f[i][j] || f[i - 1][(j + k - x) % k];
				}
			}
		}
	} 
	long long ans = sum_a / k + sum_b / k;
	for(int x = 1; x < k; x++)
	{
		if(f[n][x])
		{
			ans = max(ans, (sum_a - x) / k + (sum_b - (k - x)) / k + 1);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

标签:std,main,Phoenix,638,sum,Codeforces,long,int,include
来源: https://www.cnblogs.com/QingyuYYYYY/p/12818614.html