【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;
}
												
											【AtCoder】AGC016的更多相关文章
- 【AtCoder】ARC092 D - Two Sequences
		
[题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...
 - 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
		
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
 - 【AtCoder】ARC 081 E - Don't Be a Subsequence
		
[题意]给定长度为n(<=2*10^5)的字符串,求最短的字典序最小的非子序列字符串. http://arc081.contest.atcoder.jp/tasks/arc081_c [算法]字 ...
 - 【AtCoder】AGC022 F - Leftmost Ball 计数DP
		
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
 - 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
		
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
 - 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分
		
[题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...
 - 【AtCoder】ARC095 E - Symmetric Grid 模拟
		
[题目]E - Symmetric Grid [题意]给定n*m的小写字母矩阵,求是否能通过若干行互换和列互换使得矩阵中心对称.n,m<=12. [算法]模拟 [题解]首先行列操作独立,如果已确 ...
 - 【Atcoder】AGC022 C - Remainder Game 搜索
		
[题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...
 - 【Atcoder】AGC 020 B - Ice Rink Game 递推
		
[题意]n个人进行游戏,每轮只保留最大的a[i]倍数的人,最后一轮过后剩余2人,求最小和最大的n,或-1.n<=10^5. [算法]递推||二分 [题解]令L(i),R(i)表示第i轮过后的最小 ...
 
随机推荐
- ubuntu 16.04 登录后黑屏
			
进入登录界面后黑屏,重新更新英伟达的显卡,也么有起作用. 解决办法,进入登录界面时,进入终端(ctrl+alt+f1),然后修改 grub配置文件,使其每次重启时都检查文件 sudo vi /etc ...
 - how to avoid inheritance abuse
			
Liskov Principle: if S is a subtype of Type T, then any objects of type T may be repalced by objects ...
 - UVA 11796
			
题意: 有两个狗, 按照 多边形跑,不知道两条狗的速度,但是狗是同时出发,同时到达终点的 输出两条狗的 最大相距距离 - 最小相距距离: 思路 : 用物理的相对运动来计算, 每次只计算 两条狗的直线 ...
 - Ex3_2 最近点对
			
原文链接http://blog.csdn.net/zyang008/article/details/6175587 分治法 1)算法描述:已知集合S中有n个点,分治法的思想就是将S进行拆分,分为2部分 ...
 - php 汉字首字母和全拼
			
<?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...
 - Codeforces Educational Codeforces Round 57 题解
			
传送门 Div 2的比赛,前四题还有那么多人过,应该是SB题,就不讲了. 这场比赛一堆计数题,很舒服.(虽然我没打) E. The Top Scorer 其实这题也不难,不知道为什么这么少人过. 考虑 ...
 - centos6.5安装python2.7、pip、numpy、scipy
			
1..安装Development Tools yum groupinstall -y 'development tools' 2.安装SSL.bz2.zlib来为Python的安装做好准备工作 yum ...
 - CSS margin属性取值
			
margin表示一个元素的外边距.取值为正值时,表示相对于正常流离邻近元素更远,而取负值时,使其更近 但是,设置margin后,四个方向的表现形式不同 自身发生移动:top.left margin-t ...
 - Oracle 中 nvl、nvl2、nullif、coalesce、decode 函数的用法详解
			
NVL(EXPR1,EXPR2) NVL2(EXPR1,EXPR2,EXPR3) NULLIF(EXPR1,EXPR2) COALESCE(EXPR1,,..,EXPRn) decode ------ ...
 - Confluence 6 自定义站点和空间布局
			
你可以通过编辑布局文件来修改 Confluence 的外观和感觉(也可以被称为装饰).编辑这些文件将会允许你对整个 Confluence 站点的外观和感觉进行修改或者仅仅是一个独立的空间. 当你对一个 ...