早上好!(00:50:13)
這次的比賽挺好的:總共做了 \(6\) 題。
A. Problem Generator
直接 map 統計就行。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int T,n,m;
char a[MAXN];
map<char,int> k;
signed main(){
scanf("%d",&T);
while(T--){
k.clear();
scanf("%d%d",&n,&m);
scanf("%s",a+1);
for(int i = 1;i<=n;i++){
k[a[i]]++;
}
int cnt = 0;
for(char i = 'A';i<='G';i++){
cnt+=max(0,m-k[i]);
}
printf("%d\n",cnt);
}
return 0;
}
B. Choosing Cubes
這道題是小丑題,讀題時有兩個坑:
- 取的不是第 \(k\) 個正方體,而是前 \(k\) 個正方體;
- 要注意 a[m] 後面有多個的情況。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int T,n,m,k,a[MAXN];
map<int,bool> apper;
signed main(){
scanf("%d",&T);
while(T--){
apper.clear();
scanf("%d%d%d",&n,&m,&k);a[n+1]=0;
for(int i = 1;i<=n;i++){
scanf("%d",a+i);
apper[a[i]]++;
}
m = a[m];
sort(a+1,a+1+n);
reverse(a+1,a+1+n);
if(a[k]>m) puts("no");
else{
if(a[k]==m&&a[k]==a[k+1]) puts("maybe");
else puts("yes");
}
}
return 0;
}
C. Sofia and the Lost Operations
判斷最後一個是否合法,前面的能否一一匹配。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int T,n,a[MAXN],m,b[MAXN],c[MAXN];
map<int,int> apper,be;
signed main(){
scanf("%d",&T);
while(T--){
apper.clear();be.clear();
scanf("%d",&n);
for(int i = 1;i<=n;i++){
scanf("%d",a+i);
}
for(int i = 1;i<=n;i++){
scanf("%d",b+i);
apper[b[i]]++;
}
scanf("%d",&m);
for(int i = 1;i<=m;i++){
scanf("%d",c+i);
be[c[i]]++;
}
if(apper.find(c[m])==apper.end()) puts("no");
else{
bool bj = 1;
for(int i = 1;i<=n;i++){
if(a[i]!=b[i]){
if(!be[b[i]]){
bj = 0;
break;
}
be[b[i]]--;
}
}
if(bj) puts("yes");
else puts("no");
}
}
return 0;
}
D. GCD-sequence
直接前驅合法,後繼合法,拼接就行。(程式碼有一點複雜,)注意的事情要注意。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int T,n,a[MAXN];
int gcd[MAXN];
bool pre[MAXN],suc[MAXN];
map<int,int> apper,be;
signed main(){
scanf("%d",&T);
while(T--){
apper.clear();be.clear();
scanf("%d",&n);
for(int i = 1;i<=n;i++){
scanf("%d",a+i);
if(i>1) gcd[i] = __gcd(a[i],a[i-1]);
}
gcd[0] = gcd[1] = 0;
gcd[n+1] = 0x3f3f3f3f;
pre[1] = pre[2] = true;
for(int i = 3;i<=n;i++){
pre[i] = pre[i-1]&&(gcd[i]>=gcd[i-1]);
}
suc[n] = suc[n-1] = true;
for(int i = n-2;i>=1;i--){
suc[i] = suc[i+1]&&(gcd[i+1]<=gcd[i+2]);
}
bool fg = 0;
for(int i = 2;i<n;i++){
int g = (__gcd(a[i-1],a[i+1]));
if(pre[i-1]&&suc[i+1]&&g>=gcd[i-1]&&g<=gcd[i+2]){
fg = 1;
break;
}
}
if(fg||pre[n-1]||suc[2]) puts("Yes");
else puts("No");
}
return 0;
}