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

相关推荐
Tisfy2 天前
LeetCode 2434.使用机器人打印字典序最小的字符串:贪心(栈)——清晰题解
leetcode·机器人·字符串·题解·贪心·
Tisfy12 天前
LeetCode 2894.分类求和并作差:数学O(1)一行解决
数学·算法·leetcode·题解
Tisfy17 天前
LeetCode 3356.零数组变换 II:二分查找 + I的差分数组
算法·leetcode·二分查找·题解·差分数组
Tisfy17 天前
LeetCode 3355.零数组变换 I:差分数组
算法·leetcode·题解·差分数组
liuzhangfeiabc18 天前
[luogu12541] [APIO2025] Hack! - 交互 - 构造 - 数论 - BSGS
c++·算法·题解
liuzhangfeiabc19 天前
[luogu12542] [APIO2025] 排列游戏 - 交互 - 博弈 - 分类讨论 - 构造
c++·算法·题解
Tisfy1 个月前
LeetCode 3341.到达最后一个房间的最少时间 I:Dijkstra算法(类似深搜)-简短清晰的话描述
leetcode··最短路·dijkstra·题解·迪杰斯特拉
Tisfy1 个月前
LeetCode 1550.存在连续三个奇数的数组:遍历
算法·leetcode·题解·数组·遍历
ArmeriaLeap1 个月前
AtCoder AT_abc405_d ABC405D - Escape Route
题解·atcoder
Tisfy1 个月前
LeetCode 1295.统计位数为偶数的数字:模拟
算法·leetcode·题解