【AtCoder】AGC014
AGC014
A - Cookie Exchanges
发现两个数之间的差会逐渐缩小,所以只要不是三个数都相同,那么log次左右一定会得到答案
#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);
}
int64 A,B,C,S;
void Solve() {
read(A);read(B);read(C);
S = A + B + C;
int cnt = 0;
while(1) {
if((A & 1) || (B & 1) || (C & 1)) {out(cnt);enter;return;}
if(A == B && B == C) {puts("-1");return;}
A = (S - A) / 2;
B = (S - B) / 2;
C = (S - C) / 2;
++cnt;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
B - Unplanned Queries
直接建一个图出来,求是否每个联通块存在欧拉回路,只需要判点度就好
#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,M;
int deg[MAXN];
void Solve() {
read(N);read(M);
int a,b;
for(int i = 1 ; i <= M ; ++i) {
read(a);read(b);
++deg[a];++deg[b];
}
for(int i = 1 ; i <= N ; ++i) {
if(deg[i] & 1) {puts("NO");return;}
}
puts("YES");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
C - Closed Rooms
我们发现进行完第一轮前K次随便走的操作,剩下的就是沿着最短的直线走到某个边上,因为可以用前一轮的破壁和下一轮的走路
所以先bfs然后看看能到的所有点离边最少要再走几轮
#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,K;
char s[805][805];
bool vis[805][805];
int dis[805][805];
queue<pii > Q;
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
void Solve() {
read(H);read(W);read(K);
for(int i = 1 ; i <= H ; ++i) {
scanf("%s",s[i] + 1);
}
for(int i = 1 ; i <= H ; ++i) {
for(int j = 1 ; j <= W ; ++j) {
if(s[i][j] == 'S') {
Q.push(mp(i,j));vis[i][j] = 1;
}
}
}
while(!Q.empty()) {
pii u = Q.front();Q.pop();
if(dis[u.fi][u.se] == K) continue;
for(int k = 0 ; k < 4 ; ++k) {
int tx = u.fi + dx[k],ty = u.se + dy[k];
if(s[tx][ty] == '.' && !vis[tx][ty]) {
Q.push(mp(tx,ty));
vis[tx][ty] = 1;
dis[tx][ty] = dis[u.fi][u.se] + 1;
}
}
}
int ans = 1000000000;
for(int i = 1 ; i <= H ; ++i) {
for(int j = 1 ; j <= W ; ++j) {
if(vis[i][j]) {
if(i == 1 || j == 1 || i == H || j == W) ans = min(ans,1);
else {
int t = min(i - 1,j - 1);
t = min(t,min(H - i,W - j));
ans = min(ans,1 + (t - 1) / K + 1);
}
}
}
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - Black and White Tree
dfs,如果一个点是叶子,那么认为这个地方是当这个点的父亲被占后,Aoki的必占点,标成1
然后不是叶子的话,数数这个点Aoki的必占点有几个,如果大于等于2个,那么证明先手会赢,如果只有1个,那么这里可以变成Takahashi可以通过一定走法必占的点,如果是0个,则这个点是Aoki必占点
如果根节点是Aoki必占点,那么先手一定会赢
再加上某个点Aoki必占点的儿子大于等于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 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;
struct node {
int to,next;
}E[MAXN * 2];
int head[MAXN],sumE,col[MAXN];
bool flag = 0;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs(int u,int fa) {
int s = 0;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
dfs(v,u);
s += col[v];
}
}
if(s == 0) col[u] = 1;
if(s >= 2) flag = 1;
}
void Solve() {
read(N);
int a,b;
for(int i = 1 ; i < N ; ++i) {
read(a);read(b);
add(a,b);add(b,a);
}
dfs(1,0);
if(col[1] || flag) puts("First");
else puts("Second");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
E - Blue and Red Tree
我们相当于选择一条蓝边和一条红边,断开之后的两边点集是一样的
那么我们逆着这个操作来,相当于加边,如果一个两个点之间用两条以上的边,那么就合成一个点即可
我们把符合要求的点扔进一个队列里,然后用一个map维护点对之间的边,给每个点开一个multiset维护这个点指向的边
启发式合并,复杂度是\(O(n \log^2 n)\)
#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;
multiset<int> to[MAXN];
map<pii,int> zz;
queue<pii > Q;
void insertEdge(int a,int b) {
if(a == b) return;
if(a > b) swap(a,b);
zz[mp(a,b)]++;
if(zz[mp(a,b)] == 2) Q.push(mp(a,b));
to[a].insert(b);to[b].insert(a);
}
void Delete(int x,int y) {
if(x > y) swap(x,y);
if(zz.count(mp(x,y))) zz.erase(mp(x,y));
}
void Merge(int x,int y) {
for(auto t : to[y]) {
Delete(t,y);
to[t].erase(to[t].find(y));
insertEdge(t,x);
}
}
void Solve() {
read(N);
int a,b,c,d;
for(int i = 1 ; i < 2 * N - 1 ; ++i) {
read(a);read(b);
insertEdge(a,b);
}
for(int i = 1 ; i < N ; ++i) {
while(1) {
if(Q.empty()) {puts("NO");return;}
pii u = Q.front();Q.pop();
if(zz.count(u)) {
if(to[u.fi].size() < to[u.se].size()) swap(u.fi,u.se);
Merge(u.fi,u.se);
break;
}
}
}
puts("YES");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
F - Strange Sorting
又是一道类似数学归纳法的题
我们看到如果删掉了1的排列如何做,如果我们排序\(2,3,4...N\)用了\(T\)次操作,那么我们找到\(T - 1\)次操作的时候在\(2,3,4...N\)这堆数最前面的数是\(f\)
如果\(1\)在\(f\)和\(2\)之间,那么证明排列\(1\)到\(N\)用\(T\)次,否则用\(T + 1\)次
首先可以用定义看出\(f \neq 2\),否则\(T\)可以是\(T - 1\)
如何求\(1\)和\(f\)和\(2\)的位置关系呢,我们先证明如下两个事实
- \(f\)只在排在\(2...N\)最前面的时候才会当高元素,否则都是矮元素
- 第一个元素肯定是高元素,考虑一个\(x\)能当高元素且不为第一个,前面肯定有一个\(y < x\),那么\(x\)排不到最前面,且\(x\)为矮元素的时候\(y\)也是矮元素,然而\(f\)排在最前面,所以\(f\)当不了中间的高元素
- \(1,2,f\)的循环位置不变,指\((a,b,c),(b,c,a),(c,a,b)\)不变
- 如果\(1\)是第一个数,\(2\)是第二个数,那么\(1\)是高元素,\(2\)是高元素,f是矮元素,顺序变为\(f,1,2\),循环顺序不变
- 如果\(1\)是第一个数,\(2\)不是第二个数,\(1\)是高元素,\(2\)是矮元素,\(f\)是矮元素,顺序变为\(2,f,1\),循环顺序不变
- 如果\(2\)是第一个数,\(2\)是高元素,\(1,f\)是矮元素,循环顺序不变
- 如果\(f\)是第一个数,\(f\)是高元素,\(1,2\)是矮元素,循环顺序不变
- 否则三个都是矮元素,循环顺序不变
由此就可以求出来\(f\)排在最前时,\(f,1,2\)的位置关系
然后边递推边求\(f\)即可
#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 300005
//#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;
int p[MAXN],pos[MAXN];
int pre[MAXN],f[MAXN],ans[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(p[i]);pos[p[i]] = i;
}
for(int i = N - 1 ; i >= 1 ; --i) {
if(ans[i + 1] == 0) {
if(pos[i] < pos[i + 1]) ans[i] = 0;
else {ans[i] = 1;f[0] = i + 1;}
}
else {
int t = ans[i + 1] - 1;
int a[3] = {i,i + 1,f[t]};
sort(a,a + 3,[](int c,int d){return pos[c] < pos[d];});
while(a[0] != f[t]) {
swap(a[2],a[0]);swap(a[1],a[2]);
}
if(a[1] == i) {
ans[i] = ans[i + 1];
}
else {
ans[i] = ans[i + 1] + 1;
f[t + 1] = i + 1;
}
}
}
out(ans[1]);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
【AtCoder】AGC014的更多相关文章
- 【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轮过后的最小 ...
随机推荐
- noi.ac 集合
A.集合 --- 题面 不知道有没有用的传送门[滑稽 就是给你一个 包含 1~n 的集合,让你求它的大小为 k 的子集 s 的 \(T^{min(s)}\) 的期望值, T 为给出值, min(s) ...
- VS2013中如何解决error C4996: 'fopen'问题
今天编写控制台应用程序时出现如下错误 error C4996: 'fopen': This function or variable may be unsafe. Consider using fop ...
- ansible笔记(10):初识ansible playbook
ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...
- CANopen--实现双电机速度同步
图1 将上图图中左边的电机和右边的电机进行速度同步,右边的电机同步左边的电机速度.这里需要知道Copley的驱动中的速度环的输入输出情况.如下图所示,速度环限制器接收速度命令信号,经限制后,产生一限制 ...
- Laravel 5.2 错误-----ReflectionException in compiled.php line 8572: Class App\Http\Controllers\Apih5\ZhaoshangController does not exist
测试的时候,报错了!想不到找了半天的问题,居然是个低级错误. <?php namespace App\Http\Controllers\Apih5; use Illuminate\Http\Re ...
- push to origin/master was rejected错误解决方案
idea中,发布项目到OSChina的Git中,当时按照这样的流程添加Git,然后push,提示:push to origin/master war rejected". 解决方案如下: 1 ...
- [转]Java微服务框架选型(Dubbo 和 Spring Cloud?)
转载于 http://www.cnblogs.com/xishuai/p/dubbo-and-spring-cloud.html 微服务(Microservices)是一种架构风格,一个大型复杂软件应 ...
- 7)django-示例(cbv)
CBV(class base view)一个url根据method方式调用相应的方法.method常用有get,post 如果是GET请求,Home类会调用get方法,如果是POST提交数据,则类会调 ...
- LuoGu P1939 【模板】矩阵加速(数列)
板子传送门 矩阵快速幂学完当然要去搞一搞矩阵加速啦 (矩阵加速相对于矩阵快速幂来说就是多了一个构造矩阵的过程) 关于怎样来构造矩阵,这位大佬讲的很好呢 构造出矩阵之后,我们再去用矩阵快速幂乘出来,取[ ...
- Confluence 6 数据库表和参考
扩展下面的链接来显示主要的表格和每一个表格的外键. 单击这里来显示/隐藏表格... AO_9412A1_AOUSER ID AO_9412A1_USER_APP_LINK USER_ID fk_ao ...