Codeforces Round #756 (Div. 3)

这篇博客详细解析了Codeforces Round #756 (Div. 3)的比赛题目,包括A. Make Even、B. Team Composition、C. Polycarp Recovers the Permutation等,涉及数论、图论和动态规划等多个知识点,适合ACM竞赛选手和编程爱好者学习。
摘要由CSDN通过智能技术生成

A. Make Even

题意:

​ 给你一个数字,你可以任意反转他的前缀,问最少反转几次可以使得这个数为偶数

分析:

​ 首先很明显的是,如果给的数中每一位都是奇数那么很明显不可能

  • 刚开始就是偶数答案为0
  • 最高位为偶数答案为1
  • 其次答案为2

代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	int T;cin>>T;
	while (T--)
	{
		int num;cin>>num;
		if (num%2==0)
		{
			cout<<"0\n";
			continue;
		}vector<int> ns;
		int cnt=0;
		while (num)
		{
			ns.push_back(num%10);
			if (num%10%2==0)++cnt;
			num/=10;
		}
		if (cnt==0)
		{
			cout<<"-1\n";
			continue;
		}
		if (ns.back()%2==0)
		{
			cout<<"1\n";
			continue;
		}
		cout<<"2\n";
	}
}

B. Team Composition: Programmers and Mathematicians

题意:

​ 有 a a a个数学家, b b b个工程师,要求组最多的 4 4 4人队伍,要求每个队伍至少有一名数学家和一名工程师

分析:

​ 如果 a = b a=b a=b,那么我们肯定两个数学家两个工程师这样组队

如果不相等的话,那么我们肯定先让人多派三个人,人少的派一个人。

代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	int T;cin>>T;
	while (T--)
	{
		int n,m;cin>>n>>m;
		if (n<m)swap(n,m);
		int cnt = min({n/3,m,(n-m)/2});
		n -= 3*cnt;
		m -= cnt;
		cout<<cnt+min(n/2,m/2)<<"\n";
	}
}

C. Polycarp Recovers the Permutation

题意:

​ 一个长为 n n n的排列,每次对比两端数字的大小,取小的数字

  • 小的数字是左端的,则放入新排列的左端
  • 小的数字是右端的,则放入新排列的右端

如此,构成了一个新排列

现在给你一个新排列,要求你构造出老排列

分析:

​ 很明显的一个结论是,最大的那个数 n n n一定在新排列的最左端过着最右端

因为,他是最大的,一定是最后被放入的

然后我们可以这样构造老排列,先在新排列找到最大的数 n n n,然后翻转其他的数

就是老排列了

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+100;
int a[maxn];
int n;

int main()
{
	ios::sync_with_stdio(0);
	int T;cin>>T;
	while (T--)
	{
		cin>>n;
		for (int i=1;i<=n;++i)cin>>a[i];
		if (a[1]==n)reverse(a+2,a+1+n);
		else if (a[n]==n)reverse(a+1,a+n);
		else 
		{
			cout<<"-1\n";
			continue;
		}
		for (int i=1;i<=n;++i)cout<<a[i]<<" ";cout<<endl;
	}
}

D. Weights Assignment For Tree Edges

题意:

​ 给你一棵树&#

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值