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 3341.到达最后一个房间的最少时间 I:Dijkstra算法(类似深搜)-简短清晰的话描述
leetcode··最短路·dijkstra·题解·迪杰斯特拉
Tisfy3 天前
LeetCode 1550.存在连续三个奇数的数组:遍历
算法·leetcode·题解·数组·遍历
ArmeriaLeap4 天前
AtCoder AT_abc405_d ABC405D - Escape Route
题解·atcoder
Tisfy14 天前
LeetCode 1295.统计位数为偶数的数字:模拟
算法·leetcode·题解
满怀101520 天前
【LeetCode】7.整数反转
leetcode·题解
Tisfy24 天前
LeetCode 2563.统计公平数对的数目:排序 + 二分查找
python·算法·leetcode·二分查找·题解·排序
Tisfy1 个月前
LeetCode 2843.统计对称整数的数目:字符串数字转换
算法·leetcode·字符串·题解
Tisfy1 个月前
LeetCode 3396.使数组元素互不相同所需的最少操作次数:O(n)一次倒序遍历
算法·leetcode·题解·数组·遍历·哈希表
Tisfy1 个月前
LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
算法·leetcode··题解
Lyrella1 个月前
luogu-P5320题解
数学·题解