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 2209.用地毯覆盖后的最少白色砖块:记忆化搜索之——深度优先搜索(DFS)
算法·leetcode·深度优先·dfs·题解·记忆化搜索·深度优先搜索
Tisfy4 天前
LeetCode 0624.数组列表中的最大距离:只关心最小最大值
算法·leetcode·题解·模拟·枚举·思维
Tisfy6 天前
LeetCode 1287.有序数组中出现次数超过25%的元素:遍历
算法·leetcode·题解·模拟·数组·遍历
Tisfy13 天前
LeetCode 0063.不同路径 II:动态规划 - 原地使用地图数组,几乎无额外空间开销
算法·leetcode·动态规划·题解·dp
Tisfy17 天前
LeetCode 0598.区间加法 II:最小值
算法·leetcode·题解·思维
Tisfy19 天前
LeetCode 0922.按奇偶排序数组 II:O(1)空间复杂度-一次遍历双指针
算法·leetcode·题解·双指针
Tisfy21 天前
LeetCode 0541.反转字符串 II:模拟
算法·leetcode·字符串·题解
Tisfy1 个月前
LeetCode 2239.找到最接近 0 的数字:遍历
算法·leetcode·题解·数组·遍历
Tisfy1 个月前
LeetCode 2266.统计打字方案数:排列组合
数学·算法·leetcode·动态规划·题解·排列组合
Archippus1 个月前
题解:AT_abc389_d [ABC389D] Squares in Circle
题解·oi