zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 187 ABCDE 题解

    A - Large Digits

    思路:签到题,读入字符串即可。

    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 1e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    int main()
    {
        string a, b;
        cin>>a>>b;
        ll sum1 = 0, sum2 = 0;
        for(int i=0; i<a.size(); i++) sum1 += a[i]-'0';
        for(int i=0; i<b.size(); i++) sum2 += b[i]-'0';
        cout<<max(sum1,sum2)<<endl;
        return 0;
    }
    
    

    B - Gentle Pairs

    思路:暴力统计,算斜率的时候直接看分子绝对值是否小于等于分母绝对值。

    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 1e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    typedef struct Pos{
        ll x;
        ll y;
    }P;
    
    P a[maxn];
    
    int main()
    {
        ll n = read();
        rep(i,1,n) a[i].x = read(), a[i].y = read();
        ll sum = 0;
        rep(i,1,n) rep(j,i+1,n)
        {
            ll up = a[i].x - a[j].x;
            ll down = a[i].y - a[j].y;
            swap(up,down);
            if(abs(up)<=abs(down) ) sum++;
        }
        cout<<sum<<endl;
        return 0;
    }
    
    

    C - 1-SAT

    思路:先Map记录,然后把有!的去掉头,看看剩下的是否存在即可。

    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 1e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    int main()
    {
        ll n  = read();
        map<string,int> Map;
        vector<string> item;
        rep(i,1,n)
        {
            string s;
            cin>>s;
            Map[s] = 1;
            item.pb(s);
        }
        int flag = 0;
        rep(i,1,n)
        {
            if(item[i-1][0]=='!')
            {
                string t(item[i-1],1,item[i-1].size());
                if(Map[t])
                {
                    cout<<t<<endl;
                    flag = 1;
                    break;
                }
            }
        }
        if(!flag) cout<<"satisfiable"<<endl;
        return 0;
    }
    
    

    D - Choose Me

    思路:这题有个小wa点就是不能直接贪心拿和大的(有可能和比较小但是其中的第一个值很大)。
    考虑因为要第二个人得分比第一个人高分,我们如果对一个人分析的话,第二个人最优的情况是全部都选上,就是所有的和。然后我们看要剔除尽可能的地点。
    每次剔除,自身-sum,对方+第一个值, 相当于我 (-sum-第一个值)。 所以我们就按照2*第一个值 + 第二个值从小到大来排个序,然后按照这个顺序来剔除直到剩余值小于等于0即可。

    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 2e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    typedef struct Voter
    {
        ll aoki;
        ll taka;
        ll sum;
        bool operator < (const Voter &a) const
        {
            return sum<a.sum;
        }
    }V;
    
    V a[maxn];
    ll sum[maxn];
    
    int main()
    {
        ll n = read();
        rep(i,1,n) a[i].aoki = read(), a[i].taka = read(), a[i].sum = 2*a[i].aoki + a[i].taka;
        sort(a+1,a+1+n);
        ll cur = 0;
        rep(i,1,n) cur += a[i].aoki + a[i].taka;
    
        rep(i,1,n)
        {
            cur -= a[i].sum;
            if(cur<=0)
            {
                cout<<n-i+1<<endl;
                break;
            }
        }
        return 0;
    }
    
    

    E - Through Path

    这题暴力不可取。因为每次询问的ab是直接相连的,所以可以从这里入手(一开始没看到是直接相连卡住了)。
    我们假设1为根。若a的深度比b小的时候,也就是a是b的父节点,那么这个时候,从跟出发的所有子树中,剔除b及其子树都可以+x。
    (那我们就add[1] += x, add[b] -= x, 表示1结点下面的所有子树都+x,b及其子树都-x。)
    因为dfs的时候可以将父节点的增值信息传递下来,所以我们询问的时候只需要记录第一次发生改变的结点就行了。
    若a是b的子树,那么这种情况只需要a的子树都+x,(即add[a] += x。)

    view code
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include <queue>
    #include<sstream>
    #include <stack>
    #include <set>
    #include <bitset>
    #include<vector>
    #define FAST ios::sync_with_stdio(false)
    #define abs(a) ((a)>=0?(a):-(a))
    #define sz(x) ((int)(x).size())
    #define all(x) (x).begin(),(x).end()
    #define mem(a,b) memset(a,b,sizeof(a))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define per(i,n,a) for(int i=n;i>=a;--i)
    #define endl '
    '
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> PII;
    const int maxn = 2e5+200;
    const int inf=0x3f3f3f3f;
    const double eps = 1e-7;
    const double pi=acos(-1.0);
    const int mod = 1e9+7;
    inline int lowbit(int x){return x&(-x);}
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
    inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
    inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
    inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
    inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
    int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
    
    vector<vector<int> > D(maxn);
    ll n;
    ll dep[maxn];
    ll add[maxn];
    ll a[maxn];
    ll b[maxn];
    
    void getDep(int cur, int step, int pre)
    {
        dep[cur] = step;
        for(int i=0; i<D[cur].size(); i++)
        {
            int v = D[cur][i];
            if(v!=pre) getDep(v,step+1,cur);
        }
    }
    
    void dfs(int cur, ll preSum, int pre)
    {
        add[cur] += preSum;
        for(int i=0; i<D[cur].size(); i++)
        {
            int v = D[cur][i];
            if(v!=pre) dfs(v,add[cur], cur);
        }
    }
    
    int main()
    {
        ll n = read();
        rep(i,1,n-1)
        {
            ll x = read();
            ll y = read();
            D[x].pb(y);
            D[y].pb(x);
            a[i] = x;
            b[i] = y;
        }
        getDep(1,1,-1);
        ll q = read();
        rep(i,1,q)
        {
            ll flag = read(), e = read(), x = read();
            if(flag==1)
            {
                if(dep[a[e]] < dep[b[e]])
                {
                    add[1] += x;
                    add[b[e]] -= x;
                }
                else
                {
                    add[a[e]] += x;
                }
            }
            else
            {
                if(dep[b[e]] < dep[a[e]])
                {
                    add[1] += x;
                    add[a[e]] -= x;
                }
                else
                {
                    add[b[e]] += x;
                }
            }
        }dfs(1,0,-1);
            rep(i,1,n) cout<<add[i]<<endl;
        return 0;
    }
    
    

  • 相关阅读:
    1046 Shortest Distance (20 分)(模拟)
    1004. Counting Leaves (30)PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
    1041 Be Unique (20 分)(hash散列)
    1036 Boys vs Girls (25 分)(查找元素)
    1035 Password (20 分)(字符串处理)
    1044 Shopping in Mars (25 分)(二分查找)
    onenote使用小Tip总结^_^(不断更新中...)
    1048 Find Coins (25 分)(hash)
    三个故事
    领导者的举止
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/14224360.html
Copyright © 2011-2022 走看看