【AtCoder】AGC016
A - Shrinking
用每个字母模拟一下就行
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
char s[105];
bool vis[30],a[105],b[105];
int N;
void Solve() {
scanf("%s",s + 1);
N = strlen(s + 1);
for(int i = 1 ; i <= N ; ++i) vis[s[i] - 'a'] = 1;
int ans = N;
for(int t = 0 ; t < 26 ; ++t) {
if(vis[t]) {
int cnt = 0;
memset(a,0,sizeof(a));
for(int i = 1 ; i <= N ; ++i) a[i] = (s[i] == 'a' + t);
while(1) {
bool f = 1;
for(int i = 1 ; i <= N - cnt; ++i) {
f = f & a[i];
}
if(f) break;
memset(b,0,sizeof(b));
for(int i = 1 ; i <= N - cnt - 1; ++i) {
b[i] = a[i] || a[i + 1];
}
++cnt;
memcpy(a,b,sizeof(a));
}
ans = min(ans,cnt);
}
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
B - Colorful Hats
如果最大值和最小值相等,要么最大值等于N - 1,否则就是最大值乘2小于N
如果相差1,最小值的个数是t
那么要满足最大值\(t + 1\leq A \leq t + (N - t) / 2\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,a[MAXN];
int tot;
void Solve() {
read(N);
int maxx = 0,minn = N;
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);maxx = max(a[i],maxx);minn = min(a[i],minn);
}
if(maxx - minn > 1) {puts("No");return;}
if(maxx == minn) {
if(maxx == N - 1) puts("Yes");
else if(maxx * 2 <= N) puts("Yes");
else puts("No");
return;
}
for(int i = 1 ; i <= N ; ++i) {
if(a[i] == minn) ++tot;
}
if(maxx <= tot) {puts("No");return;}
if(N - tot >= 2 * (maxx - tot)) {
puts("Yes");return;
}
puts("No");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
C - +/- Rectangle
如果\(H\)是\(h\)的倍数并且\(W\)是\(w\)的倍数,那么无解
否则认为\(H\)不是\(h\)的倍数,以0开始标号,每个\(h\)的倍数的行都填成正数\(1000(h - 1) - 1\),其他行都填成-1000
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int H,W,h,w;
void Solve() {
read(H);read(W);read(h);read(w);
if(H % h != 0) {
puts("Yes");
for(int i = 0 ; i < H ; ++i) {
for(int j = 0 ; j < W ; ++j) {
if(i % h == 0) {out(1000 * (h - 1) - 1);}
else out(-1000);
space;
}
enter;
}
}
else if(W % w != 0) {
puts("Yes");
for(int i = 0 ; i < H ; ++i) {
for(int j = 0 ; j < W ; ++j) {
if(j % w == 0) {out(1000 * (w - 1) - 1);}
else out(-1000);
space;
}
enter;
}
}
else {
puts("No");
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - XOR Replace
相当于最后多了一个位置,是a的异或和,然后每次相当于交换最后多的这个位置上的数和选中的数
然后我们按照权值建点,a和b对应位置的数值连边,起点为a的异或和,终点为多出来的数,这两点中间连一条边,我们需要每个联通块有欧拉回路,所以统计奇数点的个数,除2是附加边的个数,然后走完一个联通块到下一个联通块中不用走回来,所以是多连一条即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int a[MAXN],b[MAXN],N,val[MAXN],tot,st,ed,deg[MAXN],cnt;
int fa[MAXN];
map<int,int> zz;
int getfa(int u) {
return fa[u] == u ? u : fa[u] = getfa(fa[u]);
}
void Init() {
read(N);
for(int i = 1 ; i <= N ; ++i) {read(a[i]);a[N + 1] ^= a[i];}
for(int i = 1 ; i <= N ; ++i) read(b[i]);
for(int i = 1 ; i <= N + 1 ; ++i) {
zz[a[i]]++;
}
}
void Solve() {
for(int i = 1 ; i <= N ; ++i) {
if(zz[b[i]] == 0) {
puts("-1");return;
}
zz[b[i]] -= 1;
}
st = a[N + 1];
for(int i = 1 ; i <= N + 1; ++i) {
val[++tot] = a[i];
if(zz[a[i]]) ed = a[i];
}
sort(val + 1,val + tot + 1);
tot = unique(val + 1,val + tot + 1) - val - 1;
st = lower_bound(val + 1,val + tot + 1,st) - val;
ed = lower_bound(val + 1,val + tot + 1,ed) - val;
for(int i = 1 ; i <= tot ; ++i) {
fa[i] = i;
}
for(int i = 1 ; i <= N ; ++i) {
if(a[i] != b[i]) {
int p = lower_bound(val + 1,val + tot + 1,a[i]) - val;
int q = lower_bound(val + 1,val + tot + 1,b[i]) - val;
++deg[p];++deg[q];
fa[getfa(p)] = getfa(q);
++cnt;
}
}
++deg[st];++deg[ed];
int p = 0;
for(int i = 1 ; i <= tot ; ++i) {
if(deg[i] & 1) ++p;
}
cnt += p / 2;
for(int i = 1 ; i <= tot ; ++i) {
if(deg[i]) {
if(getfa(i) != getfa(st)) {
fa[getfa(i)] = getfa(st);
cnt++;
}
}
}
out(cnt);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}
E - Poor Turkeys
如果一个点集S在t次操作后不会被消灭,有四种情况
\(x_{t} \in S,y_{t} \in S\)那么一定会被消灭
如果\(x_{t} \in S,y_{t} \notin S\)那么前\(t - 1\)次\(S \cup y_{t}\)必须得存在
如果\(x_{t} \notin S,y_{t} \in S\)那么前\(t - 1\)次\(S\cup x_{t}\)必须得存在
如果\(x_{t} \notin S,y_{t} \notin S\)那么前\(t - 1\)次\(S\)必须存在
所以对于一个点\(v\)是否最后可以存在,那么可以用\({v}\)往后倒推
对于一个点对必须存在
\(v\)不会被吃掉
\(u\)不会被吃掉
操作两个点对得到的集合没有相同的点
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M;
int x[MAXN],y[MAXN];
bool S[405][405],eat[405];
void Solve() {
read(N);read(M);
for(int i = 1 ; i <= M ; ++i) {
read(x[i]);read(y[i]);
}
for(int i = 1 ; i <= N ; ++i) {
S[i][i] = 1;
for(int j = M ; j >= 1 ; --j) {
if(S[i][x[j]] && S[i][y[j]]) {eat[i] = 1;break;}
else if(S[i][x[j]] && !S[i][y[j]]) S[i][y[j]] = 1;
else if(!S[i][x[j]] && S[i][y[j]]) S[i][x[j]] = 1;
}
}
int ans = 0;
for(int i = 1 ; i <= N ; ++i) {
for(int j = i + 1 ; j <= N ; ++j) {
if(!eat[i] && !eat[j]) {
bool flag = 1;
for(int h = 1 ; h <= N ; ++h) {
if(S[i][h] && S[j][h]) {flag = 0;break;}
}
ans += flag;
}
}
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
F - Games on DAG
用\(dp[S]\)表示给\(S\)之间的点加边使得1和2的sg函数相同
我们可以把\(S\)划分成两个点集\(T\)和\(U\)保证这两个点集要么都有12,要么12一个都没有
\(U\)之间没有边
\(U\)到\(T\)的边随意
\(T\)到\(U\)必须有一条边
然后\(T\)之间的边连边方式是\(dp[T]\)
答案是\(2^{M} - dp[2^{N} - 1]\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,M;
int c[16],dp[(1 << 15) + 5],cnt[(1 << 15) + 5],pw[100005];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int lowbit(int x) {
return x & (-x);
}
void update(int &x,int y) {
x = inc(x,y);
}
void Solve() {
read(N);read(M);
pw[0] = 1;
for(int i = 1 ; i <= M ; ++i) pw[i] = mul(pw[i - 1],2);
for(int i = 1 ; i < (1 << N) ; ++i) cnt[i] = cnt[i - lowbit(i)] + 1;
int x,y;
for(int i = 1 ; i <= M ; ++i) {
read(x);read(y);
c[x] |= 1 << y - 1;
}
dp[0] = 1;
for(int S = 1 ; S < (1 << N) ; ++S) {
if((S & 3) != 3 && (S & 3) != 0) continue;
for(int T = S; T ; T = (T - 1) & S) {
if((T & 3) != 3 && (T & 3) != 0) continue;
int d = 1;
for(int i = 1 ; i <= N ; ++i) {
if(T & (1 << i - 1)) d = mul(d,pw[cnt[c[i] & (S ^ T)]]);
if((S ^ T) & (1 << i - 1)) d = mul(d,pw[cnt[c[i] & T]] - 1);
}
d = mul(d,dp[S ^ T]);
update(dp[S],d);
}
}
out(inc(pw[M],MOD - dp[(1 << N) - 1]));enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}