A
模拟
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
void solve(){
int n;
cin>>n;
std::vector<int> a(n+1),b(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
if(b[i]<a[i]){
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}
B
经典的DFS求连通块的问题
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
const int N=110;
char a[N][N];
int n,m;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
void dfs(int x,int y){
for(int i=0;i<4;i++){
int x1=dx[i]+x;
int y1=dy[i]+y;
if(x1<1||x1>n||y1<1||y1>m){
continue;
}
if(a[x1][y1]!='B'){
continue;
}
else{
a[x1][y1]='.';
dfs(x1,y1);
}
}
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='B'){
ans++;
dfs(i,j);
}
}
}
cout<<ans<<endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}
C
一般求求最大值最小值往往是可以用二分解决的
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
const int N=110;
int n,m,x,y;
bool check(int h){
int ans1=h/x;
int ans2=h/y;
int ans3=h/(x*y);
int maxn1=max(0ll,n-(ans2-ans3));
int maxn2=max(0ll,m-(ans1-ans3));
return maxn1+maxn2<=h-ans1-ans2+ans3;
}
void solve(){
cin>>n>>m>>x>>y;
int l=0;
int r=1e9+10;
while(l<r){
int mid=(l+r)/2;
if(check(mid)){
r=mid;
}
else{
l=mid+1;
}
}
cout<<l<<endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}