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


    A - New Generation ABC

    思路:分段讨论

    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();
        if(n>=1&&n<=125) cout<<4<<endl;
        else if(n>=126&&n<=211) cout<<6<<endl;
        else cout<<8<<endl;
        return 0;
    }
    
    

    B - How many?

    思路:暴力枚举即可。

    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 s = read(), t = read();
        ll ans = 0;
        rep(i,0,s) rep(j,0,s) rep(k,0,s) if(i+j+k<=s&&i*j*k<=t) ans++;
        cout<<ans<<endl;
        return 0;
    }
    
    

    C - Distribution

    思路:前i个的最优解,由前i-1个最优解 + (S_{i-1}) 过来,或者直接选当前(T_{i}), 由于是个环,所以多复制一段1-n到a数组后面,遍历1-2n即可得到每个最优解。

    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 = 4e5+200;
    const ll inf=0x3f3f3f3f3f3f3f3f;
    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} };
    
    ll each[maxn];
    ll s[maxn];
    ll t[maxn];
    
    int main()
    {
        ll n = read();
        rep(i,0,2*n+1) each[i] = 1e15;
        ll pre = inf;
        rep(i,1,n) s[i] = read(); rep(i,n+1,2*n) s[i] = s[i-n];
        rep(i,1,n) t[i] = read(); rep(i,n+1,2*n) t[i] = t[i-n];
        rep(i,1,n*2)
        {
            each[i] = min(min(t[i],each[i-1]+s[i-1]),each[i]);
        }
        rep(i,n+1,n*2) cout<<each[i]<<endl;
        return 0;
    }
    
    

    D - Sum of Maximum Weights

    思路:考虑从权值从小到大逐个每个边,使得当前边讨论时,只需要加上这条边两端点的个数相乘,再乘上权值((ans+= numx* numy * val))。这一步操作利用并查集维护即可。

    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;
        ll val;
        bool operator < (const Pos &a) const
        {
            return val < a.val;
        }
    }P;
    P a[maxn];
    
    ll fa[maxn];
    ll num[maxn];
    
    ll get(ll x)
    {
        return fa[x] = (fa[x]==x?x:get(fa[x]));
    }
    
    void solve()
    {
        ll n = read();
        rep(i,1,n) fa[i] = i, num[i] = 1;
        rep(i,1,n-1) a[i].x = read(), a[i].y = read(), a[i].val = read();
        sort(a+1,a+1+n-1);
        ll ans = 0;
        rep(i,1,n-1)
        {
            ll x = a[i].x, y = a[i].y, val = a[i].val;
            ll fx = get(x), fy = get(y);
            if(fx != fy)
            {
                ll numx = num[fx];
                ll numy = num[fy];
                ans += numx*numy*val;
                fa[fy] = fx;
                num[fx] += num[fy];
               //cout<<i<<' '<<x<<' '<<y<<' '<<val<<' '<<numx<<' '<<numy<<' '<<val<<endl;
            }
        }
        cout<<ans<<endl;
    }
    
    
    int main()
    {
        solve();
        return 0;
    }
    
    

    E - Packing Under Range Regulations

    思路:先考虑暴力对每个点遍历时的策略:对于当前i,我要对所有L<=i的区间里面,贪心挑一个R最小的(R小要尽快处理掉),这样对每个i都如此维护的话,一旦发现这个挑出来的R<i,则不合法,最后检验是否每个区间都被挑完了。由于i范围很大,所以考虑优化,发现要满足L<=i,有很多点可以直接跳着走,所以当发现没有区间可以挑的时候,可以直接跳到后面最近的L上,如此往复即可。

    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 Pos
    {
        ll l;
        ll r;
        bool operator < (const Pos &a) const
        {
            return r > a.r;
        }
    }P;
    P a[maxn];
    
    bool cmp(P a, P b)
    {
        return a.l < b.l;
    }
    
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read();
            rep(i,1,n) a[i].l = read(), a[i].r = read();
            sort(a+1,a+1+n,cmp);
            priority_queue<P,vector<P> > q;
            ll cnt = 0;
            int id = 1;
            int pos = 1;
            while(id<=n||q.size())
            {
                while(id<=n&&a[id].l<=pos) q.push(a[id]), id++;
                if(q.size()&&q.top().r<pos) break;
                if(q.size()&&q.top().r>=pos) q.pop(), cnt++;
                if(q.empty()&&id<=n) pos = a[id].l;
                else if(q.size()) pos++;
            }
            puts(cnt==n?"Yes":"No");
        }
        return 0;
    }
    
    

  • 相关阅读:
    This project references NuGet package(s) that are missing on this computer
    Industry Engineer
    winform
    OPC
    groupbox 下的datagridview的列标题字体修改混乱
    vs远程调试
    nuget pack 时不包含依赖包(而不是引用项目的dll,区别于IncludeReferencedProjects)
    Inno Setup入门(二十二)——Inno Setup类参考(8)
    学习GitHub
    python3.4读取excel数据绘图
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/15143502.html
Copyright © 2011-2022 走看看