【解题报告】CF EDU DIV2 #ROUND 108 A~D

【解题报告】CF EDU DIV2 #ROUND 108 A~D

比赛链接
晚上准备打,结果貌似网炸了,离谱。十来分钟了都没加载出题目,镜像也蜜汁挂掉,所以早上起来打的虚拟场。晚上好像篮球比赛,室友貌似挺兴奋,估计也不适合打cf吧hh。

9分钟切A,30分钟切B,C没磨出来,1:30切D,排名3900+。发现目前B老卡很久,B/C模拟也没模出来,这个是很大的问题,看来思维还是不够活跃,读题不OK

A. Red and Blue Beans

思路
简单贪心,要让差值尽可能小,那就尽可能多分几组
代码

// Problem: A. Red and Blue Beans
// Contest: Codeforces - Educational Codeforces Round 108 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1519/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY

*/
int T;
void solve(int C)
{
	//NEW DATA CLEAN
	
	//NOTE!!!
	LL r,b,d;cin>>r>>b>>d;
	LL m=min(r,b),n=max(r,b),t;
	if(m)
	{
		if(n%m==0)t=n/m-1;
		else t=n/m;
		
		if(t<=d)puts("YES");
		else puts("NO");
	}
	else
	{
		if(d)puts("YES");
		else puts("NO");
	}
	
	
}

int main()
{
	#ifdef MULINPUT
		scanf("%d",&T);
		for(int i=1;i<=T;i++)solve(i);
	#else
		solve(1);
	#endif
	return 0;
}

B.The Cake Is a Lie

思路
开局还以为是类似数字三角的DP,后来发现单纯找规律。按照题目要求把边权图画出来。
在这里插入图片描述

发现由于对称性,无论怎么走,都要花费m*n-1,那直接判断就可以了。
代码

// Problem: B. The Cake Is a Lie
// Contest: Codeforces - Educational Codeforces Round 108 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1519/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY

*/
int T;
void solve(int C)
{
	//NEW DATA CLEAN
	
	//NOTE!!!
	LL n,m,k;cin>>n>>m>>k;
	if(k==m*n-1)puts("YES");
	else puts("NO");
	
}

int main()
{
	#ifdef MULINPUT
		scanf("%d",&T);
		for(int i=1;i<=T;i++)solve(i);
	#else
		solve(1);
	#endif
	return 0;
}

C. Berland Regional

在这里插入图片描述

思路
感觉自己还是没能很好的控制时间和空间复杂度,开了2e5*2e5二维数组,写了个铁MLE的,然后因为vector坐标偏移问题,前缀和部分没写好。
参考了这篇博客
大致思路:对每个大学的队伍按照能力值从大到小排队,然后预处理前缀和。当队伍规模为k的时候我们取前缀和 s [ m / k ∗ k ] s[m/k*k] s[m/kk]即可
现在来优化一下先前爆掉的二维数组,可以用vector优化掉一维度

代码

// Problem: C. Berland Regional
// Contest: Codeforces - Educational Codeforces Round 108 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1519/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY

*/
int T;
const int N=2e5+10;
LL u[N],ans[N];
vector<LL>g[N];
void solve(int C)
{
	//NEW DATA CLEAN
	memset(ans,0,sizeof ans);
	//NOTE!!!
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>u[i];
		g[i].clear();
	}
	for(int i=1;i<=n;i++)
	{
		LL x;cin>>x;
		g[u[i]].push_back(x);
	}
	for(int i=1;i<=n;i++)
		sort(g[i].rbegin(),g[i].rend());;
	
	for(int i=1;i<=n;i++)//前缀和
	{
		int len=g[i].size();
		for(int j=1;j<len;j++)//因为vector是从0开始的所以<len
		{
			g[i][j]=g[i][j-1]+g[i][j];//直接在原数组上搞,很妙避免了vector因为开局为0造成的困扰
		}
	}
	for(int i=1;i<=n;i++)
	{
		int len=g[i].size();
		for(int j=1;j<=len;j++)//队伍规模为j
		{
			ans[j]+=g[i][(len)/j*j-1];//别忘记-1
		}
	}
	
	for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
	puts("");
}

int main()
{
	#ifdef MULINPUT
		scanf("%d",&T);
		for(int i=1;i<=T;i++)solve(i);
	#else
		solve(1);
	#endif
	return 0;
}

D. Maximum Sum of Products

思路
难得写出的D,是一道区间DP
f [ l ] [ r ] f[l][r] f[l][r]为,翻转区间 [ l , r ] [l,r] [l,r]答案的改变量
状态转移 f [ l ] [ r ] = f [ l + 1 ] [ r − 1 ] + a [ l ] ∗ b [ r ] + a [ r ] ∗ b [ l ] − a [ l ] ∗ b [ l ] − a [ r ] ∗ b [ r ] f[l][r]=f[l+1][r-1]+a[l]*b[r]+a[r]*b[l]-a[l]*b[l]-a[r]*b[r] f[l][r]=f[l+1][r1]+a[l]b[r]+a[r]b[l]a[l]b[l]a[r]b[r]
然后原来的答案加上最大改变量就可以了。

在其他博客里看到另外一种思考方式是枚举中心扩展点,然后模拟扩展过程即可
代码

// Problem: D. Maximum Sum of Products
// Contest: Codeforces - Educational Codeforces Round 108 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1519/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
//#define MULINPUT
/*DATA & KEY

*/
int T;
const int N=5050;
LL a[N],b[N],c[N],s[N];
LL f[N][N];
void solve(int C)
{
	//NEW DATA CLEAN
	
	//NOTE!!!
	int n;cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];
	for(int i=1;i<=n;i++)
	{
		c[i]=a[i]*b[i];
		s[i]=s[i-1]+c[i];
	}
	
	LL ans=0;
	for(int len=2;len<=n;len++)
		for(int l=1;l+len-1<=n;l++)
		{
			int r=l+len-1;	
			f[l][r]=f[l+1][r-1]+a[l]*b[r]+a[r]*b[l]-a[l]*b[l]-a[r]*b[r];
			ans=max(ans,f[l][r]);
		}
	cout<<ans+s[n]<<endl;
}

int main()
{
	#ifdef MULINPUT
		scanf("%d",&T);
		for(int i=1;i<=T;i++)solve(i);
	#else
		solve(1);
	#endif
	return 0;
}

反思

A:

每组最大差值尽可能小,多分几组

B:

(1,1)~(n,m)走方格问题,可以观察图是否具有对称性

C:

vector处理前缀和可以直接在原数组上做前缀和,避免开局为0的情况

D:

区间DP可以把答案改变量作为DP数组的意义,转移方式可以从中间向两边扩展

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值