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

相关推荐
Tisfy15 分钟前
LeetCode 2411.按位或最大的最小子数组长度:一次倒序遍历
数据结构·算法·leetcode·题解·位运算·遍历
Tisfy8 天前
LeetCode 1695.删除子数组的最大得分:滑动窗口(哈希表)
算法·leetcode·散列表·题解·双指针·滑动窗口·哈希表
Tisfy12 天前
LeetCode 3202.找出有效子序列的最大长度 II:取模性质(动态规划)
算法·leetcode·动态规划·题解·模运算
WebGoC开发者13 天前
C++题解(37) 信息学奥赛一本通1318:【例5.3】自然数的拆分
c++·算法·青少年编程·题解
XuYueming14 天前
圆方树学习笔记 —— 一种关于点双连通分量的思考方式
题解·lca·缩点·tarjan·圆方树·点双连通分量·树链剖分·记录 & 心得·仙人掌·理论 / 算法·bzoj·hydro·图的连通性·动态 dp / ddp
syzyc20 天前
[ABC267F] Exactly K Steps
数据结构·动态规划·题解
Tisfy2 个月前
LeetCode 2434.使用机器人打印字典序最小的字符串:贪心(栈)——清晰题解
leetcode·机器人·字符串·题解·贪心·
Tisfy2 个月前
LeetCode 2894.分类求和并作差:数学O(1)一行解决
数学·算法·leetcode·题解
Tisfy2 个月前
LeetCode 3356.零数组变换 II:二分查找 + I的差分数组
算法·leetcode·二分查找·题解·差分数组
Tisfy2 个月前
LeetCode 3355.零数组变换 I:差分数组
算法·leetcode·题解·差分数组