A. Make it White
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b,n;
string s;
cin>>n>>s;
for(int i=0;i<n;i++){
if(s[i]=='B'){
a=i;
break;
}
}
for(int i=n-1;i>=0;i--){
if(s[i]=='B'){
b=i;
break;
}
}
cout<<b-a+1<<"\n";
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}
B. Following the String 暴力
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
map<char,int>mp;
for(int i=1;i<=n;i++){
int x=a[i];
for(char j='a';j<='z';j++){
if(mp[j]==x){
cout<<j;
mp[j]++;
break;
}
}
}
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}
C. Choose the Different Ones! 贪心+思维
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,m,k;
cin>>n>>m>>k;
map<int,int>mp1;
map<int,int>mp2;
for(int i=1;i<=n;i++){
int x;
cin>>x;
if(x<=k)mp1[x]++;
}
for(int i=1;i<=m;i++){
int x;
cin>>x;
if(x<=k)mp2[x]++;
}
int t1=0,t2=0,t3=0;
for(int i=1;i<=k;i++){
if(mp1[i]&&!mp2[i])t1++;
else if(!mp1[i]&&mp2[i])t2++;
else if(mp1[i]&&mp2[i])t3++;
else{
cout<<"NO\n";
return;
}
}
for(int i=0;i<=t3;i++){
if(t1+i==t2+(t3-i)&&t1+i==k/2){
cout<<"YES\n";
return;
}
}
cout<<"NO\n";
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}
D. Find the Different Ones!
有点像dp // a[i]代表了与a[i]不同值的a[j]的最近下表j且j< i
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
int b[N];
void solve(){
int n;
cin>>n;
int c=0;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=2;i<=n;i++){
if(a[i-1]!=a[i]){
c=i-1;
}
b[i]=c;
}
int q;
cin>>q;
for(int i=1;i<=q;i++){
int l,r;
cin>>l>>r;
if(b[r]<l){
cout<<-1<<" "<<-1<<"\n";
}else{
cout<<b[r]<<" "<<r<<"\n";
}
}
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}
标签:std,923,int,void,namespace,Codeforces,using,Div,include
From: https://www.cnblogs.com/yufan1102/p/18014463