AtCoder Beginner Contest 166

传送门:https://atcoder.jp/contests/abc166

前三题看代码 🕵️‍♂️

A

#include<bits/stdc++.h>
using namespace std;

int main(){
	string t; cin>>t; if(t=="ABC") puts("ARC"); else puts("ABC");
	return 0;
}

B

#include<bits/stdc++.h>
using namespace std;

const int N=105;

int buc[N];

int main(){
	int n, m; cin>>n>>m;
	
	for(int i=1; i<=m; i++){
		int d; cin>>d;
		while(d--){
			int t; cin>>t;
			buc[t]++;
		}
	}
	
	int res=0;
	for(int i=1; i<=n; i++) res+=buc[i]==0;
	cout<<res<<endl;
	
	return 0;
}

C

#include<bits/stdc++.h>
using namespace std;

inline void read(int &x) {
    int s=0;x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    x*=s;
}

const int N=1e5+5;
bool ok[N];
int w[N];

int main(){
	int n, m; cin>>n>>m;
	for(int i=1; i<=n; i++) read(w[i]), ok[i]=true;
	
	while(m--){
		int u, v; read(u), read(v);
		if(w[u]>=w[v]) ok[v]=false;
		if(w[v]>=w[u]) ok[u]=false;
	}
	
	int res=0;
	for(int i=1; i<=n; i++) res+=ok[i];
	cout<<res<<endl;
	
	return 0;
}

D

发现 a,b 不会很大,直接枚举储存即可。

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/b
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;

#define int long long

#define x first
#define y second

inline void read(int &x) {
    int s=0;x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    x*=s;
}

map<int, PII> mp;

int p5(int x){
	return x*x*x*x*x;
}

void init(){
	rep(i,0,1000) rep(j,-1000,1000) mp[p5(i)-p5(j)]={i, j};
}

signed main(){
	init();
	int x; cin>>x;
	cout<<mp[x].x<<' '<<mp[x].y<<endl;
	
    return 0;
}

E

先把每个数入桶,然后从左到右扫,更新桶,更新答案。

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/b
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;

inline void read(int &x) {
    int s=0;x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    x*=s;
}

const int N=2e5+5;

int buc[N], w[N];

int main(){
	int n; cin>>n;
	rep(i,1,n){
		read(w[i]); int t=i-w[i];
		if(t>0) buc[t]++;  
	}
	
	ll res=0;
	rep(i,1,n){
		int t=i-w[i];
		if(t>0) buc[t]--;                  
		int cur=w[i]+i; if(cur>n) continue;
		res+=buc[cur];
	}
	cout<<res<<endl;
	
    return 0;
}

F

分析

自然的想法是对于两个数,哪个大就减小哪个,然后让另一个增大。

基于此,我们可以初步地写出代码:(t 是当前操作)

int main(){
	int n; int a, b, c; cin>>n>>a>>b>>c;
	
	bool ok=true;
	vector<char> res;
	while(n--){
		string t; cin>>t;
		if(t=="AB"){
			if(a>b) res.pb('B'), a--, b++;
			else{
				res.pb('A');
				a++, b--;
				if(b<0) ok=false;
			}
		}
		else if(t=="AC"){
			if(a>c) res.pb('C'), a--, c++;
			else{
				res.pb('A');
				a++, c--;
				if(c<0) ok=false;
			}
		}
		else if(t=="BC"){
			if(b>c) res.pb('C'), b--, c++;
			else{
				res.pb('B');
				b++, c--;
				if(c<0) ok=false;
			}
		}
	}
	
	puts(ok? "Yes": "No");
	if(ok) for(auto i: res) cout<<i<<endl;
	
    return 0;
}

但这样还是不够的,因为上面的程序没有考虑到两个数相等时候的具体情况:

比如对于 t=="AB" ,我们在 a=b 时只是无脑地将 a+1,b1 ,这样就会下面的数据卡:

2 1 1 0
AB
BC

那如何处理这种情况呢?

我们可以先将操作记录下来,当出现两个数相等的情况时,我们考察下一个操作是什么。

例如对于 t=="AB" ,下一个操作为 t=="BC" 时,说明
b 会再次被用到,那么我们就将 b+1

类似地,我们可以将完整代码写出:

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)

typedef long long ll;

inline void read(int &x) {
    int s=0;x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    x*=s;
}

string buf[100005];

int main(){
	int n; int a, b, c; cin>>n>>a>>b>>c;
	
	rep(i,1,n) cin>>buf[i]; // 储存操作
	buf[n+1]="##"; // 边界
	
	bool ok=true; // 判断所有操作结束后是否是 Yes
	vector<char> res; // 记录答案
	rep(i,1,n){
		string t=buf[i], nxt=buf[i+1]; // 记录当前操作和下一个操作
		if(t=="AB"){
			if(a>b) res.pb('B'), a--, b++;
			else if(a==b){
				if(!a) ok=false;
				if(nxt=="BC"){
					a--, b++;
					res.pb('B');
				}
				else a++, b--, res.pb('A');
			}
			else{
				res.pb('A');
				a++, b--;
				if(b<0) ok=false;
			}
		}
		else if(t=="AC"){
			if(a>c) res.pb('C'), a--, c++;
			else if(a==c){
				if(!a) ok=false;
				if(nxt=="BC"){
					a--, c++;
					res.pb('C');
				}
				else a++, c--, res.pb('A');
			}
			else{
				res.pb('A');
				a++, c--;
				if(c<0) ok=false;
			}
		}
		else if(t=="BC"){
			if(b>c) res.pb('C'), b--, c++;
			else if(b==c){
				if(!b) ok=false;
				if(nxt=="AC"){
					b--, c++;
					res.pb('C');
				}
				else b++, c--, res.pb('B');
			}
			else{
				res.pb('B');
				b++, c--;
				if(c<0) ok=false;
			}
		}
	}
	
	puts(ok? "Yes": "No");
	if(ok) for(auto i: res) cout<<i<<endl;
	
    return 0;
}
posted @   HinanawiTenshi  阅读(83)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示