Codeforces Round 942 (Div. 2)

A. Contest Proposal

暴力模拟

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=110;
int b[N];
int n;
void solve() {
	cin>>n;
	deque<int>q;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q.push_back(x);
	}
	for(int i=0;i<n;i++) cin>>b[i];
	int cnt=0;
	while(1){
		bool ok=false;
		for(int i=0;i<n;i++){
			if(q[i]>b[i]){
				q.push_back(b[i]);
				cnt++;
				sort(q.begin(),q.end());
				q.pop_back();
				ok=true;
				break;
			}
		}
		if(!ok) break;
	}
	cout<<cnt<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    cin>>t;
	while(t--) {
		solve();
	}
	return 0;

B. Coin Games

博弈

Alice先行

操作:取走一枚正面的硬币,翻转相邻的两个硬币

当某人遇到的局面没有正面则输

如果可以列出所有情况,那就不是问题,耐心慢慢列出所有情况

当U个数为奇数时,Alice赢,否则Bob赢

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n;
string s;
void solve() {
	cin>>n;
	cin>>s;
	int cnt=0;
	for(int i=0;i<n;i++){
		if(s[i]=='U') cnt++;
	}
	if(cnt%2) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}

C. Permutation Counting

尽量平均分

使得n个数的最小值尽量大

假设最终的最小值是x

然后结果就是x+(n-1)*(x-1)

再加上还有大于x的个数

那么如何使得最小值最大呢?可以二分

check x,如果不足x,则补为x,看k是否够

注意二分的范围

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n,k;
bool check(int x){
	int res=k;
	for(int i=1;i<=n;i++){
		if(a[i]<x) res-=(x-a[i]);
		if(res<0) return false;
	}
	return true;
}
void solve() {
	cin>>n>>k;
	int sum=0;
	for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
	sum+=k;
	int l=1,r=2e12;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(check(mid)) l=mid;
		else r=mid-1;
	}
//	cout<<l<<endl;
	int ans=l+(n-1)*(l-1);
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(a[i]>l) cnt++;
	}
	for(int i=1;i<=n;i++){
		if(a[i]<l) k-=(l-a[i]);
	}
	ans+=cnt;
	ans+=min(n-cnt,k);
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}

D1. Reverse Card (Easy Version)

首先,a是b的倍数

暴力枚举a,然后枚举a的因数,这样达到2e9了,会超时

需要优化

假设a+b=kb
b
gcd(a,b)=bb
则k
b=k1b b

则k=k1b
a+b=k1
b*b

即a+b是b方的倍数

枚举b,然后a+b最大为n+b,所以a+b要满足是b方的倍数,直接通过整除来确定个数,最后多了0 1的情况,减去

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n,m;
void solve() {
	cin>>n>>m;
	int ans=0;
	for(int i=1;i<=m;i++){
		ans+=(n+i)/(i*i);
	}
	ans--;
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}
相关推荐
OOJO1 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
别或许3 小时前
1、高数----函数极限与连续(知识总结)
算法
田梓燊3 小时前
code 560
数据结构·算法·哈希算法
笨笨饿3 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
kobesdu3 小时前
综合强度信息的激光雷达去拖尾算法解析和源码实现
算法·机器人·ros·slam·激光雷达
weixin_413063214 小时前
记录 MeshFlow-Online-Video-Stabilization 在线稳像
算法·meshflow·实时防抖
会编程的土豆4 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式
炘爚4 小时前
深入解析printf缓冲区与fork进程复制机制
linux·运维·算法
迈巴赫车主5 小时前
蓝桥杯19724食堂
java·数据结构·算法·职场和发展·蓝桥杯
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 78. 子集 | C++ 回溯算法题解
c++·算法·leetcode