zoukankan      html  css  js  c++  java
  • 【AtCoder】ARC079

    ARC079题解

    C - Cat Snuke and a Voyage

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pdi pair<db,int>
    #define mp make_pair
    #define pb push_back
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define eps 1e-8
    #define mo 974711
    #define MAXN 200005
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    typedef double db;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        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;
    bool vis[MAXN];
    vector<int> to[MAXN];
    void Solve() {
        read(N);read(M);
        int a,b;
        vis[N] = 1;
        for(int i = 1 ; i <= M ; ++i) {
    	read(a);read(b);
    	if(a > b) swap(a,b);
    	if(b == N) vis[a] = 1;
    	to[a].pb(b);to[b].pb(a);
        }
        if(vis[1]) {puts("POSSIBLE");return;}
        for(auto k : to[1]) {
    	if(vis[k]) {puts("POSSIBLE");return;}
        }
        puts("IMPOSSIBLE");
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
        return 0;
    }
    

    D - Decrease (Contestant ver.)

    由于发现一个1,2,3,4,5,6,7....N的序列一次操作后可以变成

    2,3,4,5,6,7...N,0的序列,这样N次过后,总会得到所有数-1的序列

    也就是,我可以进行那么多次,序列可以构造成(K / N)开始加1到N的序列,就是可以进行(lfloor frac{K}{N} floor N)那么多次了

    剩下的就从头开始逆着往回加即可

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pdi pair<db,int>
    #define mp make_pair
    #define pb push_back
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define eps 1e-8
    #define mo 974711
    #define MAXN 200005
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    typedef double db;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        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;
    int64 a[55],K;
    void Solve() {
        read(K);
        N = 50;
        a[1] = K / N;
        for(int i = 2 ; i <= N ; ++i) {
    	a[i] = a[i - 1] + 1;
        }
        int t = K % N;
        for(int i = 1 ; i <= t ; ++i) {
    	a[i] += N;
    	for(int j = 1 ; j <= N ; ++j) {
    	    if(i != j) a[j]--;
    	}
        }
        out(N);enter;
        for(int i = 1 ; i <= N ; ++i) {
    	out(a[i]);space;
        }
        enter;
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
        return 0;
    }
    

    E - Decrease (Judge ver.)

    进行一次操作我们直接把最大的扣到N以下,暴力进行几次复杂度不会太大

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pdi pair<db,int>
    #define mp make_pair
    #define pb push_back
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define eps 1e-8
    #define mo 974711
    #define MAXN 200005
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    typedef double db;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        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;
    int64 a[55],ans;
    void Solve() {
        read(N);
        for(int i = 1 ; i <= N ; ++i) read(a[i]);
        while(1) {
    	int t = 1;
    	for(int i = 2 ; i <= N ; ++i) {
    	    if(a[i] > a[t]) t = i;
    	}
    	if(a[t] < N) break;
    	int64 k = (a[t] - (N - 1) - 1) / N + 1;
    	a[t] -= k * N;
    	ans += k;
    	for(int i = 1 ; i <= N ; ++i) {
    	    if(i != t) a[i] += k;
    	}
        }
        out(ans);enter;
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
        return 0;
    }
    

    F - Namori Grundy

    若连通还每个点就一个入度,那这是一个有向的基环外向树

    把环上挂着的数给dp完,相当于每次对于儿子取一个未出现过的最小值一样的操作

    然后环上的点要么取自己未出现过的最小值,要么取把这个最小值填上之后下一个没出现过的

    也就是环上前一个点如果选择下一个点的第一选项,下一个点必须选择第二选项

    把环上每个点拆成两个点,按照这种关系连边,出现大小恰好为原来环点数的环时合法,如果无环或有一个二倍点数的环就不合法

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pdi pair<db,int>
    #define mp make_pair
    #define pb push_back
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define eps 1e-8
    #define mo 974711
    #define MAXN 200005
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    typedef double db;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        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,p[MAXN],deg[MAXN],val[MAXN];
    int id[MAXN][2],tot,cir;
    vector<int> t[MAXN],to[MAXN * 2];
    vector<int> son[MAXN];
    queue<int> q;
    int dfn[MAXN * 2],low[MAXN * 2],sta[MAXN * 2],instack[MAXN * 2],idx,top;
    bool flag = 0;
    void Tarjan(int u) {
        dfn[u] = low[u] = ++idx;
        sta[++top] = u;instack[u] = 1;
        for(auto v : to[u]) {
    	if(!dfn[v]) {Tarjan(v);low[u] = min(low[v],low[u]);}
    	else if(instack[u] == 1){
    	    low[u] = min(low[u],dfn[v]);
    	}
        }
        if(low[u] == dfn[u]) {
    	int k = 0;
    	while(1) {
    	    int x = sta[top--];
    	    ++k;
    	    instack[x] = 2;
    	    if(x == u) break;
    	}
    	if(k == cir) flag = 1;
        }
    }
    void Solve() {
        read(N);
        for(int i = 1 ; i <= N ; ++i) {
    	read(p[i]);
    	deg[p[i]]++;
        }
        for(int i = 1 ; i <= N ; ++i) {
    	if(!deg[i]) q.push(i);
        }
        while(!q.empty()) {
    	int u = q.front();q.pop();
    	sort(son[u].begin(),son[u].end());
    	son[u].erase(unique(son[u].begin(),son[u].end()),son[u].end());
    	int m = 0;
    	while(m < son[u].size()) {
    	    if(son[u][m] != m) break;
    	    ++m;
    	}
    	son[p[u]].pb(m);
    	if(!--deg[p[u]]) {
    	    q.push(p[u]);
    	}
        }
        for(int i = 1 ; i <= N ; ++i) {
    	if(deg[i]) {
    	    sort(son[i].begin(),son[i].end());
    	    son[i].erase(unique(son[i].begin(),son[i].end()),son[i].end());
    	    int m = 0,pos = 0;
    	    
    	    while(1) {
    		if(pos >= son[i].size() || son[i][pos] != m) {
    		    t[i].pb(m);
    		    if(t[i].size() < 2) {++m;}
    		    else break;
    		}
    		else {pos++;++m;}
    	    }
    	    id[i][0] = ++tot;id[i][1] = ++tot;
    	    ++cir;
    	}
        }
        for(int i = 1 ; i <= N ; ++i) {
    	if(deg[i]) {
    	    int f = p[i];
    	    to[id[f][0]].pb(id[i][t[f][0] == t[i][0]]);
    	    to[id[f][1]].pb(id[i][t[f][1] == t[i][0]]);
    	}
        }
        for(int i = 1 ; i <= tot ; ++i) {
    	if(!dfn[i]) Tarjan(i);
        }
        if(flag) puts("POSSIBLE");
        else puts("IMPOSSIBLE");
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
        return 0;
    }
    
  • 相关阅读:
    Win32中安全的子类化(翻译)
    OJ题目JAVA与C运行效率对比
    关协同过滤
    Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
    GDI+简单现实文字旋转
    opencv 2.46与visual studio 2012 配置方法
    Emacs助力PowerShell
    ARC forbids explicit message send of 'autorelease'错误
    Event处理
    复制中发布服务器和订阅服务器内容不一致的解决办法
  • 原文地址:https://www.cnblogs.com/ivorysi/p/10883706.html
Copyright © 2011-2022 走看看