C. Vasya and Templates
模拟+讨论,细节比较多。
没有写,贴了一份CF上最短的代码
#include "bits/stdc++.h"
using namespace std;
string s,a,b;
int mp[26],seen[26],k,T;
bool solve(int i, int smaller, int greater){
if((smaller&&greater) || i==s.size()) return 1;
if(mp[s[i]]!=-1){
if(!greater&&mp[s[i]]<a[i] || !smaller&&mp[s[i]]>b[i])
return false;
return solve(i+1,smaller|mp[s[i]]<b[i],greater|mp[s[i]]>a[i]);
} else{
for(int c=(greater?0:a[i]);c<=(smaller?k-1:b[i]);++c) if(!seen[c]){
mp[s[i]] = c, seen[c] = 1;
if(solve(i+1,smaller|c<b[i],greater|c>a[i])) return 1;
mp[s[i]] = -1,seen[c] = 0;
}
return 0;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
for(cin>>T;T--;cout<<'\n'){
cin>>k>>s>>a>>b;
for(auto &c:s) c-='a';
for(auto &c:a) c-='a';
for(auto &c:b) c-='a';
memset(mp,-1,sizeof(mp));
memset(seen,0,sizeof(seen));
if(solve(0,0,0)){
cout<<"YES\n";
for(int i=0;i<k;cout<<char(mp[i++]+'a'))
for(int j=0;j<k && mp[i]==-1;++j) if(!seen[j])
mp[i] = j, seen[j] = 1;
}
else cout<<"NO";
}
}
D. Rock-Paper-Scissors Champion
某个人赢的必要条件是它的两侧都满足下面两个条件之一:
- 不存在能严格赢他的人
- 存在能严格输给他的人
树状数组维护一下即可。
代码贴的:
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long ll;
const int N=2e5+10,M=3;
int n,Q,ans;
char s[N];
set<int>st[M];
int read()
{
int ret=0,f=1;char c=getchar();
while(!isdigit(c)) {
if(c=='-')f=0;c=getchar();}
while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
return f?ret:-ret;
}
struct BIT
{
#define lowbit(x) (x&(-x))
int c[N];
void update(int x,int v){
for(;x<N;x+=lowbit(x))c[x]+=v;}
int query(int x){
int res=0;for(;x;x-=lowbit(x))res+=c[x];return res;}
}bit[M];
int id(char c){
if(c=='R')return 0;else if(c=='S')return 1;return 2;}
int calc(int p)
{
int win=(p+1)%M,lose=(p-1+M)%M;
if(st[p].empty()) return bit[win].query(n);
if(st[lose].empty()) return 0;
return bit[win].query(n)-bit[win].query(max(*st[p].rbegin(),*st[lose].rbegin