zoukankan      html  css  js  c++  java
  • AtCoder Grand Contest 014 题解

    A - Cookie Exchanges 模拟

    Problem Statement

    Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:
    Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.
    This action will be repeated until there is a person with odd number of cookies in hand.
    How many times will they repeat this action? Note that the answer may not be finite.
    这道题就是模拟一个过程:
    每次都进行下面的操作.
    如果所有人的饼干都是偶数,就折半分给其他两个人,否则结束循环.
    然后问循环次数,-1表示无限次.

    Constraints

    • (1leq A,B,C leq 10^9)

    Solution

    直接模拟就好了.
    貌似这东西可以证明其复杂度是有保障的.

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    inline void read(ll &x){
        x=0;static char ch;static bool flag;flag = false;
        while(ch=getchar(),ch<'!');if(ch=='-') ch=getchar(),flag = true;
        while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
    }
    #define rg register int
    #define rep(i,a,b) for(rg i=(a);i<=(b);++i)
    #define per(i,a,b) for(rg i=(a);i>=(b);--i)
    const int lim = 100000000;
    int main(){
        ll a,b,c;read(a);read(b);read(c);
        int ans = 0;
        ll tmpa,tmpb,tmpc;
        while(ans <= lim){
            tmpa = tmpb = tmpc = 0;
            bool flag = false;
            if(!(a&1)) tmpb += a >> 1,tmpc += a >> 1;
            else break;
            if(!(b&1)) tmpa += b >> 1,tmpc += b >> 1;
            else break;
            if(!(c&1)) tmpa += c >> 1,tmpb += c >> 1;
            else break;
            if(a == tmpa && b == tmpb && c == tmpc){
                puts("-1");
                return 0;
            }a = tmpa;b = tmpb;c = tmpc;++ ans;
        }
        if(ans <= lim) printf("%d
    ",ans);
        else puts("-1");
        return 0;
    }
    

    B - Unplanned Queries

    Problem Statement

    Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
    First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
    Then, Aoki gave him M queries. The i-th of them is as follows:
    Increment the number written at each edge along the path connecting vertices ai and bi, by one.
    After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
    Determine whether there exists a tree that has the property mentioned by Takahashi.
    定义在树上从u走到v时会将所经过的所有的边上的权都+1,初始权为0.
    给出若干次的起点和终点.现在知道最终树上的所有边权均为偶数,问是否存在这么一课树。

    Constraints

    • (2 leq N leq 105)
    • (1 leq M leq 105)
    • (1 leq ai,bi leq N)
    • (ai eq bi)

    Solution

    这道题直接构造出以1为根,剩下的所有的点的父亲都是1的树即可.
    你要问为什么我只能说我也不知道,考场上抱着写写试试的心态没想到就过了.

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    inline void read(int &x){
        x=0;static char ch;static bool flag;flag = false;
        while(ch=getchar(),ch<'!');if(ch=='-') ch=getchar(),flag = true;
        while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
    }
    #define rg register int
    #define rep(i,a,b) for(rg i=(a);i<=(b);++i)
    #define per(i,a,b) for(rg i=(a);i>=(b);--i)
    const int maxn = 100010;
    int a[maxn];
    int main(){
        int n,m;read(n);read(m);
        int u,v;
        rep(i,1,m){
            read(u);read(v);
            if(u != 1) ++ a[u];
            if(v != 1) ++ a[v];
        }
        bool flag = false;
        rep(i,2,n) if(a[i] & 1) {flag = true;break;}
        puts(flag ? "NO" : "YES");
        return 0;
    }
    
  • 相关阅读:
    我的git笔记
    我的 Sublime Text 2 笔记
    在Windows系统配置Jekyll
    纯CSS3打造七巧板
    JavaScript原型之路
    基于Grunt构建一个JavaScript库
    How React Works (一)首次渲染
    hadoop2.4 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    pyinstaller打包python源程序访问hive
    hadoop balance均衡datanode存储不起作用问题分析
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6918969.html
Copyright © 2011-2022 走看看