Educational Codeforces Round 97 (Rated for Div. 2)

这篇博客包含了五个不同的编程题目,涉及营销方案判断、字符串反转、最短路径优化、最小高度树构造和序列升序改造。每道题都展示了在算法和数据结构上的应用,如动态规划、二分查找和背包问题。博主通过分析和解决这些题目,探讨了如何提高代码效率和解决问题的策略。
摘要由CSDN通过智能技术生成

A Marketing Scheme

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int T;
int x,y;
int main(){
	cin>>T;
	while(T--){
		cin>>x>>y;
		if(x*2>y)
			cout<<"YES"<<endl;
		else 
			cout<<"NO"<<endl;
	}
} 

B. Reverse Binary Strings

记录前面不匹配后面匹配的节点

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int T,n;
char s[maxn];
int main(){
	cin>>T;
	int now;
	int t1,t2;
	bool a1[maxn];
	bool a2[maxn];
	int ans1,ans2;
	int ans;
	while(T--){
		int n;
		cin>>n;
		getchar();
		scanf("%s",s);
		//puts(s);
		
		t1=0;
		ans1=0;ans2=0;
		for(int i=0;i<n;i++){
			if((s[i]-'0')==t1){
				a1[i]=true;
			}else{
				a1[i]=false;
			}
			a2[i]=!a1[i];
			t1=(t1+1)%2;
			
		}
		for(int i=1;i<n;i++){
			if(a1[i]==true&&a1[i-1]==false)
				ans1++;
			if(a2[i]==true&&a2[i-1]==false)
				ans2++;
		}
		if(a1[n-1]==false)
			ans1++;
		else
			ans2++;
		ans=min(ans1,ans2);
		cout<<ans<<endl;
		
	}
} 

C. Chef Monocarp

分析一下可以发现取餐的顺序一定是ti小的先
然后dp[i][j]表示时间i内取了j个餐的最小代价,因为一个时间只能取一次餐
所以dp[i][j]由dp[i-1][j]和dp[i-1][j-1]转移而来
wa了好多发后来发现时间上限是2*n不是n

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=210;
int T,n;
int a[maxn];
int dp[2*maxn][2*maxn];
int dis[2*maxn][2*maxn];
int main(){
	cin>>T;
	while(T--){
		memset(dp,inf,sizeof(dp));
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(int i=0;i<=2*n;i++){
			dp[i][0]=0;
		}
		dp[0][0]=0;
		for(int i=1;i<=2*n;i++){
			for(int j=1;j<=n;j++){
				dp[i][j]=min(dp[i-1][j],(dp[i-1][j-1]+abs(i-a[j])));
			}
		}
		cout<<dp[2*n][n]<<endl;
	}
} 

别人改成一维dp的代码
我是真没看出来这是个背包。。

D. Minimal Height Tree

假设树可以铺满,按dfs思路逐层计算
(cf题序不是按难度排了么?)

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=2e5+10;
int T,n;
int a[maxn];
int l[maxn];
int num[maxn];
int main(){
	cin>>T;
	int temp;
	int ans;
	int now;
	a[0]=0;
	while(T--){
		cin>>n;
		memset(l,0,sizeof(l));
		now=1;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]<a[i-1])
				now++;
			l[now]++;
		}
		int s=1;
		int e=1;
		int sum=0;
		int ans=0;
		while(1){
			ans++;
			for(int i=s;i<=e;i++)
				sum+=l[i];
			s=e+1;
			e=sum;
			if(sum==n)
				break;
		}
		cout<<ans<<endl;
	}
}  

E. Make It Increasing

做法应该是求出包含那几个固定点位,且数字之间留够相应空间的最长上升子序列长度,留够相应空间就是比如在i=2的位置是2,那在i=4的位置起码是4,而不能是3。
对于留空的问题,我们可以直接将数字减去下标,那么我们要求的就是包括这几个固定节点的最长不下降子序列长度

#include <bits/stdc++.h>
using namespace std;   
#define inf 0x3f3f3f3f;
const int maxn=5e5+100;
int a[maxn],b[maxn],d[maxn],n,m;
bool check(){
	for(int i=2;i<=m;i++)
		if(a[b[i]]<a[b[i-1]])
			return false;
	return true;
}
int solve(int l,int r)
{
	int len=1;
	d[len]=a[l];
	for(int i=l+1;i<=r;i++)
	{
		if(a[i]>=d[len])
			d[++len]=a[i];
		else
		{
			int j=upper_bound(d+1,d+1+len,a[i])-d;
			if(j!=1)
				d[j]=a[i];
		}
	}
	int pos=upper_bound(d+1,d+1+len,a[r])-d-1;
	return (r-l+1)-pos;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",a+i);
		a[i]-=i;
	}
	a[0]=-inf,a[n+1]=inf;
	b[0]=0,b[m+1]=n+1;
	for(int i=1;i<=m;i++)
		scanf("%d",b+i);
	if(!check())
		return 0*puts("-1");
	int ans=0;
	for(int i=1;i<=m+1;i++)
		ans+=solve(b[i-1],b[i]);
	printf("%d\n",ans);
    return 0;
}

标题

题意:
给 n 个数的序列 ai,求有多少种 n 个数的排列 pi,使得
a p i max ⁡ j = 1 i − 1 a p j ∉ ( 1 2 , 2 ) \frac{a_{p_i}}{\max_{j=1}^{i-1} a_{p_j}}\notin \left(\frac 12, 2\right) maxj=1i1apjapi/(21,2)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值