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;
}
相关推荐
KarrySmile2 分钟前
Day17--二叉树--654. 最大二叉树,617. 合并二叉树,700. 二叉搜索树中的搜索,98. 验证二叉搜索树
数据结构·算法·二叉树·二叉搜索树·合并二叉树·最大二叉树·验证二叉搜索树
凤年徐4 分钟前
【数据结构与算法】21.合并两个有序链表(LeetCode)
c语言·数据结构·c++·笔记·算法·链表
程序员老冯头12 分钟前
第三十二节 MATLAB函数
数据结构·算法·matlab
lifallen17 分钟前
hadoop.yarn 带时间的LRU 延迟删除
java·大数据·数据结构·hadoop·分布式·算法
jdlxx_dongfangxing18 分钟前
2024 年 NOI 最后一题题解
c++·noi
近津薪荼3 小时前
c++详解(宏与内联函数,nullptr)
开发语言·c++
淮北4944 小时前
STL学习(十一、常用的算数算法和集合算法)
c++·vscode·学习·算法
糖葫芦君4 小时前
玻尔兹曼分布与玻尔兹曼探索
人工智能·算法·机器学习
摸鱼仙人~4 小时前
Redis 数据结构全景解析
数据结构·数据库·redis
AA陈超6 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 #06-11:游戏后效果执行
c++·游戏·ue5·游戏引擎·虚幻