AtCoder ABC377 A-D题解

这次感觉难度偏低(A B C都是水题)。

比赛链接:ABC377

Problem A:

Code

#include <bits/stdc++.h>
using namespace std;
bool ok[10];
int main(){
	string S;
	cin>>S;
	for(int i=0;i<S.size();i++)
		ok[S[i]-'A']=true;
	for(int i=0;i<3;i++){
		if(!ok[i]){
			cout<<"N0"<<endl;
			return 0;
		}
	}
	cout<<"Ye5"<<endl;
	return 0;
}

Problem B:

Code

#include <bits/stdc++.h>
using namespace std;
bool ok[10][10];
string S[10];
void print(){
	for(int i=0;i<8;i++){
		for(int j=0;j<8;j++)
			cout<<ok[i][j]<<' ';
		cout<<endl;
	}
}
int main(){
	memset(ok,true,sizeof(ok));
	for(int i=0;i<8;i++){
		cin>>S[i];
		for(int j=0;j<8;j++){
			if(S[i][j]=='#'){
				ok[i][j]=false;
				for(int k=0;k<8;k++)
					ok[i][k]=false;
				for(int k=0;k<8;k++)
					ok[k][j]=false;
			}
		}
		print();
	}
	int ans=0;
	for(int i=0;i<8;i++){
		for(int j=0;j<8;j++)
			ans+=ok[i][j];
	}
	cout<<ans<<endl;
	return 0;
}

Problem C:

Code

#include <bits/stdc++.h>
using namespace std;
const int dx[]={0,2,1,-1,-2,-2,-1,1,2};
const int dy[]={0,1,2,2,1,-1,-2,-2,-1};
int main(){
	int N,M;
	cin>>N>>M;
	long long ans=1ll*N*N;
	//cout<<ans<<endl;
	set<pair<int,int>> st;
	for(int i=1;i<=M;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<9;j++){
			int x=a+dx[j];
			int y=b+dy[j];
			cout<<"第"<<i<<"组第"<<j<<"个:"<<x<<' '<<y<<endl;
			if(0<x && x<=N && 0<y && y<=N && st.find(make_pair(x,y))==st.end()){
				//cout<<"???"<<endl;
				ans--;
				st.insert(make_pair(x,y));
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

Problem D:

Sol

首先,如果(l,r)满足题意,那么(l+1,r)肯定也满足题意。因此,存在一个d_r,使得:

  • 对于每一个r,当且仅当d_r\leq l\leq r,都有(l,r)满足要求。

因此,只要找到所有的d_r,那么答案就是\sum_{r=1}^{M} (r-d_r+1)

接下来是求解d_r的过程:

假设我们知道d_{r-1}要求d_r。如果没有 i 使得R_i=r,那么d_r=d_{r-1},否则d_r=max(d_{r-1},L_{max}+1),其中L_{max}是最大的满足R_i=rL_i

因此我们可以通过递推求出d_r,时间复杂度为\Theta(N+M)

Code

//十年OI一场空,后面忘了
#include <bits/stdc++.h>
using namespace std;
const int MAX=200005;
int d[MAX]={1};
int main(){
	int N,M;
	cin>>N>>M;
	for(int i=1;i<=N;i++){
		int L,R;
		cin>>L>>R;
		d[R]=max(d[R],(long long)L+1);
	}
	for(int r=1;r<=M;r++)
		d[r]=max(d[r],d[r-1]);
	int ans=0;
	for(int r=1;r<=M;r++)
		ans+=(r-d[r]+1);
	cout<<ans<<endl;
	return 0;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值