Codeforces Round 964 (Div. 4)

Codeforces Round 964 (Div. 4)

标题

CodeForces 1999 A. A+B Again?

时限

1 second

空限

256 megabytes

题目描述

给定一个两位数的正整数 \(n\),求其数字之和。

输入

第一行包含一个整数 \(t\)\(1\leq t\leq 90\))——测试用例的数量。

每个测试用例的唯一一行包含一个两位数的正整数 \(n\)\(10\leq n\leq 99\))。

输出

对于每个测试用例,输出一个整数——\(n\) 的数字之和。

eazy.

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;
char s[maxn];

void solve(){
	cin>>s;
	int cnt=0;
	for(int i=0;s[i];i++){
		cnt+=s[i]-'0';
	}
	cout<<cnt<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();
		
	}
	return 0;
}

标题

CodeForces 1999 B. Card Game

时限

2 seconds

空限

256 megabytes

题目描述

苏内特和斯拉夫人玩纸牌游戏。游戏规则如下:

由于苏尼特和斯拉夫人不是最好的朋友,你需要计算苏尼特最终获胜的游戏可能发生的方式。为了更好地理解,请查看注释部分

输入

第一行包含一个整数 \(t\)\(1\leq t\leq 10^4\))——测试用例的数量。

每个测试用例的第一行也是唯一一行包含 \(4\) 整数 \(a_1\)\(a_2\)\(b_1\)\(b_2\)\(1\leq a_1、a_2、b_1、b_2\leq 10\)),其中 \(a_1\)\(a_2\) 分别表示 Suneet 拥有的卡片,\(b_1\)\(b_2\) 分别表示 Slavic 拥有的卡片。

输出

对于每个测试用例,输出一个整数——考虑所有可能的游戏,Suneet 将赢得的游戏数。

这题有坑点,如果 Suneet 一局打平,一局获胜,也是他胜,1:0,2:0 都要记录。

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;
int a[maxn],b[maxn];

void solve(){
	cin>>a[0]>>a[1]>>b[0]>>b[1];
	int cnt=0;
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			int cnt1=0,cnt2=0;
			if(a[i]>b[j]) cnt1++;
			if(a[i^1]>b[j^1]) cnt1++;
			if(a[i]<b[j]) cnt2++;
			if(a[i^1]<b[j^1]) cnt2++;
			if(cnt1>cnt2) cnt++;
		}
	}
	cout<<cnt<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();

	}
	return 0;
}

标题

CodeForces 1999 C. Showering

时限

2 seconds

空限

256 megabytes

题目描述

作为一名计算机科学专业的学生,亚历克斯面临着一个艰巨的挑战——洗澡。他试图每天洗澡,但尽管他尽了最大的努力,总是有挑战。他洗澡需要花费 \(s\) 分钟,而一天只有 \(m\) 分钟!

他已经为当天安排了 \(n\) 的任务。任务 \(i\) 表示为间隔 \((l_i\)\(r_i)\),这意味着 Alex 很忙,无法在该时间间隔内洗澡(在 \(l_i\)\(r_i\) 之间的任何时间点)没有两项任务重叠。 考虑到所有 \(n\) 的时间间隔,亚历克斯那天能洗澡吗?换句话说,亚历克斯会有一个长度至少为 \(s\) 的空闲时间间隔吗

输入

第一行包含一个整数 \(t\)\(1\leq t\leq 10^4\))——测试用例的数量。

\(1\leq n\leq 2\cdot 10^5\), \(1\leq s,m\leq 10^9\), \(0\leq l_i<r_i\leq m\)

所有测试用例中 \(n\) 的总和不超过 \(2 \times 10^5\)

模拟找 \(\ge s\) 的间隔即可。

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;
int s,m;
int l[maxn],r[maxn];
void solve(){
	cin>>n>>s>>m;
	for(int i=1;i<=n;i++){
		cin>>l[i]>>r[i];
	}
	for(int i=1;i<=n;i++){
		if(l[i]-r[i-1]>=s){
			cout<<"Yes\n";
			return;
		}
	}
	if(m-r[n]>=s){
		cout<<"Yes\n";
		return;
	}
	cout<<"No\n";
	return;
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();
		
	}
	return 0;
}

标题

CodeForces 1999 D. Slavic's Exam

时限

2 seconds

空限

256 megabytes

题目描述

斯拉维奇的考试很难,需要你的帮助才能通过。以下是他正在努力解决的问题:

存在一个字符串 \(s\),它由小写英文字母组成,可能有零个或多个 “”。

斯拉夫语被要求将每个 “” 更改为小写英文字母,使字符串 \(t\) 成为字符串 \(s\) 的子序列(不一定是连续的)。

输出任何这样的字符串,或者说,如果不存在符合条件的字符串,则不可能输出。

如果可能有多个答案,您可以输出其中任何一个。

贪心地将问号变为当前 \(t\) 中第一个没有在 \(s\) 中出现的字符。

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;
char s[maxn],t[maxn];
int suf[maxn][26];
void solve(){
	cin>>s>>t;
	int lens=strlen(s),lent=strlen(t),cnt=0;
	for(int i=lens-1;i>=0;i--){
		if(s[i]=='?'){cnt++;continue;}
		for(int j=0;j<26;j++){
			suf[i][j]=suf[i+1][j]+(s[i]-'a'==j);
		}
	}
	int pos=0;
	for(int i=0;i<lens;i++){
		if(pos>=lent) break;
		if(s[i]==t[pos]) pos++;
		else if(s[i]=='?'){
			s[i]=t[pos];
			pos++;
		}
	}
	if(pos!=lent){
		cout<<"No\n";
		return;
	}
	cout<<"Yes\n";
	for(int i=0;i<lens;i++){
		if(s[i]=='?') cout<<'a';
		else cout<<s[i];
	}
	cout<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();
		
	}
	return 0;
}

标题

CodeForces 1999 E. Triple Operations

时限

1 second

空限

256 megabytes

题目描述

Ivy 在黑板上写下了从 \(l\)\(r\) 的所有整数。

在手术中,她会做以下事情:

Ivy 需要多少操作才能使董事会上的所有数字等于 0?我们有证据表明这总是可能的。

输入

第一行包含一个整数 \(t\)\(1\leq t\leq 10^4\))——测试用例的数量。

每个测试用例的唯一一行包含两个整数 \(l\)\(r\)\(1\leq l<r\leq 2\cdot 10^5\))。

输出

对于每个测试用例,输出一个整数——使电路板上的所有数字等于 \(0\) 所需的最小操作次数。

首先将 \(l\) 变成 \(0\),然后用 \(l\) 去将其他东西变成 \(0\),答案为 \(cnt_l+\sum\limits_{i=l}^{r} cnt_i\),中间那坨用前缀和预处理即可,\(cnt\) 千万不要用 log,精度爆炸,喜提罚时。要直接除法模拟。

时间复杂度 \(O(r+t)\)

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;
int l,r;
int pre[maxn];
void solve(){
	cin>>l>>r;
	int ans=pre[r]-pre[l],ll=l;
	while(ll){
		ll/=3;
		ans+=2;
	}
	cout<<ans<<'\n';
	return;
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	for(int i=1;i<=200000;i++){
		int sum=0,now=i;
		while(now){
			now/=3;sum++;
		}
		pre[i]=pre[i-1]+sum;
	}
	while(T--){
		solve();
	}
	return 0;
}

标题

CodeForces 1999 F. Expected Median

时限

3 seconds

空限

256 megabytes

题目描述

Arul 有一个长度为 \(n\) 的 ** 二进制 ** 数组 。

他将取这个数组中长度为 \(k\)\(k\) 是奇数)的所有子序列,并找到它们的中值

所有这些值的总和是多少?

由于该总和可能非常大,因此将其以 \(10^9+7\) 的模输出。换句话说,将此总和除以 \(10^9+7\) 后的余数打印出来。

标题

CodeForces 1999 G1. Ruler (easy version)

时限

1 second

空限

256 megabytes

题目描述

这是问题的简单版本。这两个版本之间的唯一区别是,在这个版本中,您最多可以进行\(\mathbf{10}\) 查询.我们有一个缺少一个数字 \(x\)\(2\leq x\leq 999\))的秘密标尺。当你测量一个长度为 \(y\) 的对象时,标尺会报告以下值:上面的标尺缺少数字 \(4\),因此它正确地将第一段测量为长度 \(3\),但错误地将第二段测量为长 \(6\),即使它实际上是 \(5\)

找到 \(x\) 的值。您最多可以提出 \(\mathbf{10}\) 个查询。

输入

每个测试包含多个测试用例。第一行输入包含一个整数 \(t\)\(1\leq t\leq 1000\))——测试用例的数量。

由于查询的面积具有单调性,所以可以二分。

每次查询 ? mid mid,时间复杂度 \(O(\log_2(999)t)=O(10t)\)

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T;

void solve(){
	int l=2,r=999,ans=0;
	while(l<=r){
		int mid=(l+r)/2;
		cout<<"? "<<mid<<' '<<mid<<'\n';
		cout.flush();
		int get=0;
		cin>>get;
		if(get==mid*mid){
			l=mid+1;
		}else{
			r=mid-1;
			ans=mid;
		}
	}
	cout<<"! "<<ans<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();

	}
	return 0;
}

标题

CodeForces 1999 G2. Ruler (hard version)

时限

1 second

空限

256 megabytes

题目描述

这是问题的硬版本。

找到 \(x\) 的值。您最多可以提出 \(\mathbf{7}\) 个查询。

输入

每个测试包含多个测试用例。第一行输入包含一个整数 \(t\)\(1\leq t\leq 1000\))——测试用例的数量。

对于一个查询 ? a b\((a<b)\),其结果有 3 种:

  • \(a\times b\),这说明缺口 \(x>b\)
  • \(a\times (b+1)\),这说明缺口 \(a<x\le b\)
  • \((a+1)\times(b+1)\),这说明缺口 \(x\le a\)

于是考虑三分。设 \(L,R\) 为三分上下界, \(l,r\) 为截点,每次查询 ? l r,然后 \(L,R\) 由上进行变动即可,但是细节多,难调

时间复杂度 \(O(\log_3(999)t)=O(7t)\)

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+3;
int n,T,m;

void solve(){
	n=999;m=1;
	while(n-m>2){
		int l=(n+2*m)/3,r=(2*n+m)/3;
		cout<<"? "<<l<<' '<<r<<'\n';
		cout.flush();
		int get;
		cin>>get;
		if(get==l*r){
			m=r;
		}else if(get==l*(r+1)){
			m=l,n=r;
		}else{
			n=l;
		}
	}
	if(n-m==2){
		cout<<"? 1 "<<m+1<<'\n';
		cout.flush();
		int get;
		cin>>get;
		if(get!=m+1) n=m+1;
		else m=m+1;
	}
	cout<<"! "<<n<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		solve();

	}
	return 0;
}
posted @ 2024-08-12 21:02  view3937  阅读(21)  评论(0编辑  收藏  举报
Title