AtCoder Beginner Contest 371
A - Jiro
思路
创建变量a,b,c;根据输入进行增加,等于1的那个即为答案
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int a,b,c;
string s;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
a=b=c=0;
cin>>s;
if(s==">")a++;else b++;
cin>>s;
if(s==">")a++;else c++;
cin>>s;
if(s==">")b++;else c++;
if(a==1)cout<<"A"<<endl;
else if(b==1)cout<<"B"<<endl;
else cout<<"C"<<endl;
return 0;
}
B - Taro
思路
创建st数组记录是否出现过长子,当读入为“M”的时候忽略即可
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
bool st[N];
int n,m;
int e;
string s;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
while(m--){
cin>>e>>s;
if(!st[e]&&s=="M"){
cout<<"Yes"<<endl;
st[e]=true;
}
else cout<<"No"<<endl;
}
return 0;
}
C - Make Isomorphic
思路
由于\(N\)较小使用邻接表来表示图,方便比较和修改,同样由此原因,我们直接采取枚举\(H\)图的排列来挨个比对找到最小代价即可;
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 10, MAX = 1e9, INF = -1e9;
int n;
int mg,mh;
int u,v;
int a[N][N];
int ans=MAX+10;
int g[N][N];
int h[N][N];
vector<int> w;
int counts(){
int num=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(g[i][j]!=h[w[i]][w[j]]){
num+=a[w[i]][w[j]];
}
}
}
return num;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>mg;
for(int i=1;i<=mg;i++){
cin>>u>>v;
g[u][v]=1;
g[v][u]=1;
}
cin>>mh;
for(int i=1;i<=mh;i++){
cin>>u>>v;
h[u][v]=1;
h[v][u]=1;
}
for(int i=1;i<=n-1;i++){
for(int j=i+1;j<=n;j++){
cin>>a[i][j];
a[j][i]=a[i][j];
}
}
w.pb(0);
for(int i=1;i<=n;i++)w.pb(i);
do{
ans=min(ans,counts());
}while(next_permutation(w.begin()+1,w.end()));
cout<<ans<<endl;
return 0;
}
D - 1D Country
思路
离散化+前缀和+二分,首先对村庄的位置进行离散化处理,对村民数量进行前缀和处理,查找时采取二分查找,时间复杂度为\(O(qlogn)\)
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n,q;
vector<int> v;
int s[N];
int e;
int l,r;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>e;
v.pb(e);
}
sort(v.begin(),v.end());
for(int i=1;i<=n;i++)cin>>s[i];
s[0]=0;
for(int i=1;i<=n;i++)s[i]+=s[i-1];
cin>>q;
while(q--){
cin>>l>>r;
int st=lower_bound(v.begin(),v.end(),l)-v.begin()+1;
int end=upper_bound(v.begin(),v.end(),r)-v.begin();
cout<<s[end]-s[st-1]<<endl;
}
return 0;
}