AtCoder ABC376A-D题解

个人觉得 ABC 变得越来越难了/kk/kk/kk

比赛链接:ABC376

Problem A:

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N,C;
	cin>>N>>C;
	for(int i=1;i<=N;i++)
		cin>>T[i];
	int ans=0,pre=-1e5;
	for(int i=1;i<=N;i++){
		if(T[i]-pre>=C){
			ans++;
			pre=T[i];
		}
	}
	cout<<ans<<endl;
	return 0;
}

Problem B:

我愿称之为史上最难 B 没有之一。

Sol

暴力模拟即可。注意细节。

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N,Q;
	cin>>N>>Q;
	int l=1,r=2,ans=0;
	while(Q--){
		char H;
		int T;
		cin>>H>>T;
		if(H=='L'){
			if((l<T && T<r) || (T<r && r<l) || (r<l && l<T)){
				for(;l!=T;l++){
					if(l==N+1)
						l=1;
					if(l==T)
						break;
					ans++;
				}
			}
			else{
				for(;l!=T;l--){
					if(l==0)
						l=N;
					if(l==T)
						break;
					ans++;
				}
			}
		}
		else{
			if((l<T && T<r) || (r<l && l<T) || (T<r && r<l)){
				for(;r!=T;r--){
					if(r==0)
						r=N;
					if(r==T)
						break;
					ans++;
				}
			}
			else{
				for(;r!=T;r++){
					if(r==N+1)
						r=1;
					if(r==T)
						break;
					ans++;
				}
			}			
		}
	}
	cout<<ans<<endl;
	return 0;
}

Problem C:

我觉得 C>D。

Sol

二分套二分。考虑二分答案。在 check 函数中又要二分第一个大于等于 的数。在代码中,我选择了二分答案的下标。

Code

cpp 复制代码
#include <bitch/stdc++.h>
using namespace std;
const int maxn=200005;
int N,A[maxn],B[maxn];
bool check(int x){
	for(int i=1;i<=N;i++){
		if(i==x)
			continue;
		if(A[i]>B[i-(i>x)])
			return false;
	}
	return true;
}
int main(){
	cin>>N;
	for(int i=1;i<=N;i++)
		cin>>A[i];
	for(int i=1;i<N;i++)
		cin>>B[i];
	sort(A+1,A+N+1);
	sort(B+1,B+N);
	int l=1,r=N;
	while(l<r){
		int mid=(l+r)>>1;
		if(check(mid))
			r=mid;
		else
			l=mid+1;
	}
	if(check(l))
		cout<<A[l]<<endl;
	else
		cout<<-1<<endl;
    return 0;
}

Problem D:

Sol

考虑 bfs。从 1 开始,只要第二次遍历到 1 就输出即可。

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=200005;
bool vis[maxn];
vector<int> graph[maxn];
queue<pair<int,int>> que;
int bfs(){
	while(!que.empty()){
		pair<int,int> u=que.front();
		que.pop();
		for(auto v:graph[u.first]){
			if(v==1)
				return (u.second+1);
			if(!vis[v]){
				que.push(make_pair(v,u.second+1));
				vis[v]=true;
			}
		}
	}
	return -1;
}
int main(){
	int N,M;
	cin>>N>>M;
	for(int i=1;i<=M;i++){
		cin>>a>>b;
		graph[a].push_back(b);
	}
	que.push(make_pair(1,0));
	cout<<bfs()<<endl;
    return 0;
}

友情提醒:不要无脑Ctrl C+Ctrl V

相关推荐
Tisfy3 天前
LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
算法·leetcode··题解
Lyrella6 天前
luogu-P5320题解
数学·题解
炒鸡码力14 天前
一道原创OI题(普及-)——ZCS的随机游走
c++·算法·题解·模拟·题目
Tisfy19 天前
LeetCode 2272.最大波动的子字符串:转为多次的最大子数组和 - 一步步思考推导
算法·leetcode·动态规划·字符串·题解
Tisfy21 天前
LeetCode 3110.字符串的分数:模拟(注意一个小细节)
算法·leetcode·字符串·题解·模拟
Tisfy1 个月前
LeetCode 0132.分割回文串 II:动态规划
leetcode·动态规划·字符串·题解·回文
Igallta_8136221 个月前
【CCF GESP 2 级】小杨的H字矩阵 洛谷 B3924
c语言·开发语言·c++·笔记·算法·题解·信息学奥林匹克竞赛
朔北之忘 Clancy1 个月前
2022 年 12 月青少年软编等考 C 语言五级真题解析
c语言·开发语言·c++·学习·算法·青少年编程·题解
Tisfy1 个月前
LeetCode 1472.设计浏览器历史记录:一个数组完成模拟,单次操作均O(1)
服务器·leetcode·浏览器·memcached·题解·模拟·数组
lskkkkkkkkkkkk1 个月前
第16届蓝桥杯模拟赛3 python组个人题解
python·蓝桥杯·题解