【AtCoder】AGC014

AGC014

連接node

發現兩個數之間的差會逐漸縮小,因此只要不是三個數都相同,那麼log次左右必定會獲得答案c++

#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

直接建一個圖出來,求是否每一個聯通塊存在歐拉回路,只須要判點度就好cookie

#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次隨便走的操做,剩下的就是沿着最短的直線走到某個邊上,由於能夠用前一輪的破壁和下一輪的走路spa

因此先bfs而後看看能到的全部點離邊最少要再走幾輪code

#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必佔點,那麼先手必定會贏get

再加上某個點Aoki必佔點的兒子大於等於2個狀況先手會贏數學

除此以外後手贏hash

#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();
}
相關文章
相關標籤/搜索