文章目录
- [A. Recovering a Small String](https://codeforces.com/contest/1931/problem/A)
- [B. Make Equal](https://codeforces.com/contest/1931/problem/B)
- [C. Make Equal Again](https://codeforces.com/contest/1931/problem/C)
- [D. Divisible Pairs](https://codeforces.com/contest/1931/problem/D)
- [E. Anna and the Valentine's Day Gift](https://codeforces.com/contest/1931/problem/E)
- [F. Chat Screenshots](https://codeforces.com/contest/1931/problem/F)
A. Recovering a Small String
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n;
void solve() {
cin>>n;
int a=max(1ll,n-26*2);
n-=a;
int b=max(1ll,n-26);
n-=b;
cout<<(char)(a+'a'-1);
cout<<(char)(b+'a'-1);
cout<<(char)(n+'a'-1);
cout<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
B. Make Equal
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n;
void solve() {
cin>>n;
int sum=0;
for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
int ave=sum/n;
int res=0;
for(int i=1;i<=n;i++){
res+=a[i];
if(res<ave*i){
cout<<"No"<<endl;
return;
}
}
cout<<"Yes"<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
C. Make Equal Again
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n;
void solve() {
cin>>n;
memset(a,0,sizeof a);
for(int i=1;i<=n;i++) cin>>a[i];
int l=1,r=n;
while(a[l+1]==a[l]) l++;//[1,r]相等
while(a[r-1]==a[r]) r--;//[r,n]相等
// cout<<l<<' '<<r<<endl;
if(l==n&&r==1){
cout<<0<<endl;
return;
}
if(a[1]==a[n]){
cout<<n-(l+n-r+1)<<endl;
return;
}
cout<<n-max(l,n-r+1)<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
D. Divisible Pairs
trick:
1.n达2e5,问满足要求的pair数量,这种题型,暴力肯定不行,做法一般是一般遍历一般用map记录个数,然后ans加上前面记录的与之匹对的个数
2.a+b是x的倍数,那么(a+b)%x=0,转化为a%x+b%x=x
3.同余方程,a-b是y的倍数,那么(a-b)%y=0,a%y=b%y
4.两两进行配对,如要满足两个要求,第一个要求是对y取模相等,第二个要求是对x取模互补,那么可以将pair放入map中,假设当前为{aa,bb},那么与之匹对的是{aa,(x-bb)%x}
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int,int>PII;
const int N=2e5+10;
int a[N],b[N],c[N];
int n,x,y;
void solve() {
cin>>n>>x>>y;
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i]%x,c[i]=a[i]%y;
map<PII,int>mp;
int ans=0;
for(int i=1;i<=n;i++){
ans+=mp[{c[i],(x-b[i])%x}];
mp[{c[i],b[i]}]++;
}
cout<<ans<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
E. Anna and the Valentine’s Day Gift
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n,m;
void solve() {
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
int sum=0;
vector<int>res;
for(int i=1;i<=n;i++){
string s=to_string(a[i]);
sum+=s.size();
int r=s.size()-1;
while(s[r]=='0') r--;
int len=s.size()-1-r;
res.push_back(len);
}
sort(res.begin(),res.end());
reverse(res.begin(),res.end());
int flag=1;
for(int i=0;i<(int)res.size();i++){
if(flag) sum-=res[i];
flag^=1;
}
if(sum>m) cout<<"Sasha"<<endl;
else cout<<"Anna"<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
F. Chat Screenshots
题意为共n个人,k个人发出聊天截图,每个人都只把自己放在最前面,然后其他人相对顺序不变,问可否确定出顺序,实际上就是第一个人不看,然后从第二个开始到最后一个,相对顺序固定,看k种截图展示的是否有矛盾
用拓扑排序
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int d[N];
int a[N];
int n,k;
void solve() {
cin>>n>>k;
for(int i=1;i<=n;i++) d[i]=0;
vector<vector<int>>e(n+1);
while(k--){
for(int i=1;i<=n;i++){
cin>>a[i];
if(i>=3) e[a[i-1]].push_back(a[i]),d[a[i]]++;
}
}
queue<int>q;
for(int i=1;i<=n;i++){
if(!d[i]) q.push(i);
}
vector<int>ans;
while(q.size()){
int t=q.front();
ans.push_back(t);
q.pop();
for(auto v:e[t]){
d[v]--;
if(!d[v]) q.push(v);
}
}
if((int)ans.size()==n) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}