# CF1450 题解

## A

$b$ 都提到最前面就好了。

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 200+5;
char str[MAXN];
int cnt[26],n;

int main(){
int T;scanf("%d",&T);
while(T--){
CLR(cnt,0);scanf("%d",&n);
scanf("%s",str+1);
FOR(i,1,n) ++cnt[str[i]-'a'];
FOR(i,1,cnt[1]) putchar('b');
FOR(i,0,25){
if(i == 1) continue;
FOR(j,1,cnt[i]) putchar('a'+i);
}
puts("");
}
return 0;
}


## B

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 100+5;
int n,x[MAXN],y[MAXN],k;

inline void Solve(){
scanf("%d%d",&n,&k);
FOR(i,1,n) scanf("%d%d",x+i,y+i);
FOR(i,1,n){
int mx = 0;
FOR(j,1,n) mx = std::max(mx,std::abs(x[i]-x[j])+std::abs(y[i]-y[j]));
if(mx <= k){
puts("1");
return;
}
}
puts("-1");
}

int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}


## C2

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 300+5;
char str[MAXN][MAXN];int n;
int mp[MAXN][MAXN];
int cnt[2][3],ps[2];

inline void Solve(){
scanf("%d",&n);CLR(cnt,0);
FOR(i,1,n){
scanf("%s",str[i]+1);
FOR(j,1,n){
if(str[i][j] != '.') ++cnt[str[i][j]=='X'][(i+j)%3];
}
}
ps[0] = ps[1] = -1;
int mn = 1e9;
FOR(i,0,2){
FOR(j,0,2){
if(i == j) continue;
if(mn > cnt[0][i]+cnt[1][j]){
mn = cnt[0][i] + cnt[1][j];
ps[0] = i;ps[1] = j;
}
}
}
FOR(i,1,n){
FOR(j,1,n){
if(str[i][j] == '.') putchar(str[i][j]);
else{
if(str[i][j] == 'O') putchar((i+j)%3 == ps[0] ? 'X' : 'O');
else putchar((i+j)%3 == ps[1] ? 'O' : 'X');
}
}
puts("");
}
}

int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}


## D

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 3e5 + 5;
int n,a[MAXN];
bool vis[MAXN];
bool f[MAXN];
int cnt[MAXN];

inline void Solve(){
scanf("%d",&n);FOR(i,1,n) cnt[i] = 0;
FOR(i,1,n) scanf("%d",a+i),vis[i] = 0,++cnt[a[i]];
FOR(i,1,n) vis[a[i]] = 1,f[i] = 0;f[0] = 1;
int sm = 0;
FOR(i,1,n) f[i] = f[i-1]&vis[i],sm += vis[i];
std::reverse(f+1,f+n+1);
int l = 1,r = n,now = 1;
while(l <= r){
if(a[l] != now && a[r] != now) break;
--cnt[now];
if(cnt[now]) break;
if(a[l] == now) ++l,++now;
else if(a[r] == now) --r,++now;
}
--now;
FOR(i,1,n-now-1) f[i] = 0;
if(sm == n) f[1] = 1;
if(vis[1]) f[n] = 1;
FOR(i,1,n) putchar('0'+f[i]);puts("");
}

int main(){
int T;scanf("%d",&T);while(T--) Solve();
return 0;
}


## E

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 200+5;

int G[MAXN][MAXN],dis[MAXN][MAXN];
int n,m;
bool flag=1;
int col[MAXN];
bool vis[MAXN];

inline void dfs(int v){
vis[v] = 1;
FOR(i,1,n){
if(G[v][i] == 1e8 || v == i) continue;
if(vis[i]) flag &= (col[v]^col[i]);
else col[i] = col[v]^1,dfs(i);
}
}

int main(){
scanf("%d%d",&n,&m);
FOR(i,1,n) FOR(j,1,n) G[i][j] = 1e8;
FOR(i,1,n) G[i][i] = 0;
while(m--){
int i,j,w;scanf("%d%d%d",&j,&i,&w);
if(!w){
G[j][i] = std::min(G[j][i],1);
G[i][j] = std::min(G[i][j],1);
}
else{
G[j][i] = std::min(G[j][i],1);
G[i][j] = std::min(G[i][j],-1);
}
}
dfs(1);
if(!flag){
puts("NO");
return 0;
}
FOR(i,1,n) FOR(j,1,n) dis[i][j] = G[i][j];
int cir = 1e9;
FOR(k,1,n){
FOR(i,1,k-1){
FOR(j,1,k-1){
if(i == j) continue;
cir = std::min(cir,G[k][i]+G[j][k]+dis[i][j]);
}
}
FOR(i,1,n){
FOR(j,1,n){
dis[i][j] = std::min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
//    FOR(i,1,n) FOR(j,1,n) if(G[i][j] != 1e9 && i != j) printf("%d %d %d\n",i,j,G[i][j]);
if(cir < 0){
puts("NO");
return 0;
}
int mx = 0,ps = -1;
FOR(i,1,n){
int c = -1e9;
FOR(j,1,n) c = std::max(c,dis[i][j]);
if(mx < c){
mx = c;ps = i;
}
}
puts("YES");
printf("%d\n",mx);
FOR(i,1,n) printf("%d%c",dis[ps][i]," \n"[i==n]);
return 0;
}


## F

$f(c)$ 表示颜色 $c$ 在端点上的出现次数，一个显然的限制是 $f(c) \leq k+2$，我们现在说明只要满足这个限制的所有切割方案都是合法的：

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 1e5 + 5;
int n,a[MAXN],f[MAXN];

inline void Solve(){
scanf("%d",&n);
FOR(i,1,n) scanf("%d",a+i),f[i] = 0;
int k = 0;
++f[a[1]];++f[a[n]];
FOR(i,2,n){
if(a[i] == a[i-1]){
++k;++f[a[i]];++f[a[i-1]];
}
}
int mx = 0,ps = -1;
FOR(i,1,n){
if(mx < f[i]-k-2){
mx = f[i]-k-2;
ps = i;
}
}
if(!mx){
printf("%d\n",k);
return;
}
int r = 0;
FOR(i,2,n) if(a[i] != a[i-1] && a[i] != ps && a[i-1] != ps) ++r;
if(mx <= r){
printf("%d\n",k+mx);
}
else{
puts("-1");
}
}

int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}


## H1

$\frac{1}{2^p}\sum_{i=0,i \equiv \frac{n}{2} \pmod 2}^n \binom p {i-c} |i-\frac{n}{2}|$

$\frac{1}{2^p}\sum_{i=0,i \equiv d \pmod 2}^n \binom p i |i-d|$

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 3e5 + 5;
const int ha = 998244353;

inline int qpow(int a,int n=ha-2){
int res = 1;
while(n){
if(n & 1) res = 1ll*res*a%ha;
a = 1ll*a*a%ha;
n >>= 1;
}
return res;
}

x += y-ha;x += x>>31&ha;
}

int fac[MAXN],inv[MAXN];

inline void prework(){
fac[0] = 1;FOR(i,1,MAXN-1) fac[i] = 1ll*fac[i-1]*i%ha;
inv[MAXN-1] = qpow(fac[MAXN-1]);ROF(i,MAXN-2,0) inv[i] = 1ll*inv[i+1]*(i+1)%ha;
}

inline int C(int n,int m){
return (n < 0 || m < 0 || n < m) ? 0 : 1ll*fac[n]*inv[m]%ha*inv[n-m]%ha;
}

char str[MAXN];
int n,m;

int main(){
prework();
scanf("%d%d",&n,&m);
scanf("%s",str+1);
int B1=0,W2=0,p=0;
FOR(i,1,n){
B1 += (str[i] == 'b' && (i&1));
W2 += (str[i] == 'w' && (!(i&1)));
p += (str[i] == '?');
}
int ans = 0;
FOR(x,0,n){
int c = C(p,x),d = std::abs(x-(n/2)+B1+W2);
if(d&1) continue;
c = 1ll*c*d%ha;
}
ans = 1ll*ans*qpow(qpow(2,p))%ha;
printf("%d\n",ans);
return 0;
}


## H2

\begin{aligned} \sum_{i=0}^n \binom p i |i-d| = \sum_{i=0}^d \binom p i d-\sum_{i=0}^d \binom p i i+\sum_{i=d+1}^n \binom p i i - \sum_{i=d+1}^n \binom p i d \end{aligned}

$\sum_{i=0}^{lim} \binom p i$

$lim$ 加一直接加上对应的项即可。

$\binom {p+1}{i} = \binom p i + \binom p {i-1}$，所以 $p$ 加一只需要额外维护一下 $0\ldots lim-1$ 的求和即可。

posted @ 2020-12-07 21:29  RainAir  阅读(45)  评论(0编辑  收藏