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;
}
相关推荐
木子.李3474 小时前
排序算法总结(C++)
c++·算法·排序算法
闪电麦坤955 小时前
数据结构:递归的种类(Types of Recursion)
数据结构·算法
freyazzr5 小时前
C++八股 | Day2 | atom/函数指针/指针函数/struct、Class/静态局部变量、局部变量、全局变量/强制类型转换
c++
小熊猫写算法er5 小时前
终极数据结构详解:从理论到实践
数据结构
Gyoku Mint5 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
纪元A梦6 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法
fpcc6 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
px不是xp6 小时前
山东大学算法设计与分析复习笔记
笔记·算法·贪心算法·动态规划·图搜索算法
-qOVOp-6 小时前
408第一季 - 数据结构 - 栈与队列的应用
数据结构