# Codeforces Round #634 (Div. 3) 题解 (全部7题)

## A. Candies and Two Sisters

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
int n;
signed main(){
while(T--){
cout<<(n-1)/2<<endl;
}
return 0;
}


## B. Construct the String

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
string s;
signed main(){
while(T--){
s=""; repeat(i,0,b)s+=char('a'+i);
while(n){
int t=min(n,b);
repeat(i,0,t)putchar(s[i]);
n-=t;
}
puts("");
}
return 0;
}


## C. Two Teams Composing

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
map<int,int> Map;
signed main(){
while(T--){
int m=0,cnt=0;
for(auto i:Map){
cnt++;
m=max(m,i.second);
}
cout<<max(min(cnt-1,m),min(cnt,m-1))<<endl;
}
return 0;
}


## D. Anti-Sudoku

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
string s;
signed main(){
int T; cin>>T;
while(T--){
repeat(i,0,9){
cin>>s;
s[s.find('1')]='2';
cout<<s<<endl;
}
}
return 0;
}


## E2. Three Blocks Palindrome (hard version)

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
//#define int ll
int a[N],s[201][N];
signed main(){
while(T--){
repeat(c,1,200+1)
s[c][n+1]=0;
repeat(c,1,200+1)
repeat_back(i,1,n+1)
s[c][i]=s[c][i+1]+(a[i]==c);
int ans=1;
repeat(i,1,n+1){
int c=a[i];
int pre=s[c][1]-s[c][i+1];
int p2=upper_bound(s[c]+1,s[c]+n+1,pre,greater<int>())-s[c]-1;

if(p2>i)
repeat(c2,1,200+1)
ans=max(ans,s[c2][i+1]-s[c2][p2]+2*pre);
}
cout<<ans<<endl;
}
return 0;
}


## F. Robots on a Grid

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=1000010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
typedef pair<int,int> pii;
map<int,pii> dir={{'L',{0,-1}},{'R',{0,1}},{'U',{-1,0}},{'D',{1,0}}};
int n,m;
vector<int> a[N]; //这个存的是反图，上一步
inline int f(int x,int y){return x*m+y;}
int vis[N]; //带时间戳的vis
bool val[N]; //是否为黑色格子
int to[N],co[N],start[N],len[N],dis[N]; //to是下一步，co是环的id，start是是否为原点，len是某id的环长，dis是走到原点的距离+1
string s;
int dcnt,cocnt,ans1,ans2; //dcnt是vis的时间戳，cocnt是当前环的id
//#define orzz(a) []{cout<<"*****"#a"*****:"<<endl;repeat(i,0,n)repeat(j,0,m)cout<<a[f(i,j)]<<" \n"[j==m-1];}()
void work1(int x0){
int x=x0;
dcnt++;
while(vis[x]==0){
vis[x]=dcnt;
x=to[x];
}
if(vis[x]==dcnt){
cocnt++; len[cocnt]=0;
while(co[x]==0){
len[cocnt]++;
ans1++;
co[x]=cocnt;
x=to[x];
}
start[x]=1;
}
}
queue<int> q;
bool bkt[N];
void work2(int x0){
dis[x0]=1;
q.push(x0);
int nowco=co[x0];
int nowlen=len[nowco];
fill(bkt,bkt+nowlen,0);
while(!q.empty()){
int x=q.front(); q.pop();
if(val[x] && bkt[dis[x]%nowlen]==0){
ans2++;
bkt[dis[x]%nowlen]=1;
}
for(auto p:a[x]){
if(dis[p]==0){
dis[p]=dis[x]+1;
q.push(p);
}
}
}
}
signed main(){
int T; cin>>T;
while(T--){
cin>>n>>m; cocnt=dcnt=0; ans1=ans2=0;
fill(vis,vis+n*m,0);
fill(co,co+n*m,0);
fill(dis,dis+n*m,0);
fill(start,start+n*m,0);
repeat(i,0,n){
cin>>s;
repeat(j,0,m)val[f(i,j)]=(s[j]!='1');
}
repeat(i,0,n){
cin>>s;
repeat(j,0,m)
to[f(i,j)]=f(i+dir[s[j]].first,j+dir[s[j]].second);
}
repeat(i,0,n*m)if(vis[i]==0)work1(i);
repeat(i,0,n*m)a[i].clear();
repeat(i,0,n*m)a[to[i]].push_back(i);
repeat(i,0,n*m)if(start[i])work2(i);
cout<<ans1<<' '<<ans2<<endl;
}
return 0;
}

posted @ 2020-04-14 01:04  axiomofchoice  阅读(606)  评论(0编辑  收藏  举报