【CF】Educational Codeforces Round 131 (Rated for Div. 2)C++代码

A. Grass Field

有一个大小为 2×2 的草地,一次可以把某行某列的草除掉(1变0)问要几次

答:就0,1,2三种答案

int t; 
cin>>t;
while(t--)
{
	int an=0;
	int x;
	for(int i=1;i<=4;i++)
	{
		cin>>x;
		if(x)an++;
	}
	if(an==0)cout<<"0\n";
	else
	if(an==4)cout<<"2\n";
	else cout<<"1\n";
}

B. Permutation

Permutation

答:要使pi*d=pi+1 的对数越多,d必然选2 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 2e5+5;
bool vis[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t; 
	cin>>t;
	while(t--)
	{
		int n;
		memset(vis,0,sizeof(vis));
		cin>>n;
		cout<<2<<'\n';
		//k=n/2; 
		int cn=0;
		for(int i=1;i<=n;i++)
		{
			if(!vis[i])
			{
				cn=i;
				while(cn<=n)
				{
					cout<<cn<<' ';
					vis[cn]=1;
					cn=cn*2;
				}
			}
		}
		cout<<'\n';
	}
	return 0;
}

C - Schedule Management

日程管理
有 n 个工人和 m 个任务。工人从 1 到 n 编号。每个任务 i 都有一个值 ai——精通该任务的工人的编号。

答:二分+贪心,枚举每个工人工作时间

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 2e5 + 5;
vector<int>v[N];
int n, m;
bool check(int k)//每个人的工作时间
{
	ll now;
	ll need = 0;
	for (int i = 1; i <= n; i++)
	{
		now = v[i].size();
	//	cout<<i<<' '<<now<<endl;
		if (now <= k)
		{
			need -= ((ll)(k - now) / 2);
		}
		else
		{
			need += (ll)(now - k);
		}
	}
	if (need <= 0)return 1;
	return 0;
}
void solve()
{
	cin >> n >> m;//n个工人,m个工作
	int x;
	for (int i = 1; i <= m; i++)
	{
		cin >> x;
		v[x].push_back(i);
	}
	int l = 0, r = m * 2;
	int ans = 0;
	while (l <= r)
	{
		int mid = (l + r) / 2;
	//	cout<<"l r "<<l<<' '<<r<<endl;
		if (check(mid))
		{
			r = mid - 1;
			ans = mid;
		}
		else
		{
			l = mid + 1;
		}
	}
	cout << ans << '\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
		memset(v, 0, sizeof(v));
	}
	return 0;
}

D - Permutation Restoration

答:另一个帖子写了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值