• Educational Codeforces Round 97 ABCD 题解


    A. Marketing Scheme

    题意:对于一个x,如果({lfloor{xover 2} floor})<= (x) (mod) (a),则满足题意。现在问你能否选出一个a使得[l,r]区间内的所有数都满足题意。

    思路:贪心,如果(2l > r),即左端点最优方案能同时满足最大的r,就输出YES,反之则NO。

    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()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll l = read(), r = read();
            ll len = l*2;
            if(len>r) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    
    

    B. Reverse Binary Strings

    题意:让你翻转一个01子串的任意子串,问最少多少步能够凑出01交替的串。(01数量各一半)。

    思路:注意到,每次翻转都不能改变翻转区间内部的交替性质(如果是11连着翻转了还是11连着),一次翻转只会改变其左右端点两边的交替状态。因此我们可以先统计出需要交换1的数量和需要交换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 = 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()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read();
            string s;
            cin>>s;
            char pre = (s[0]=='1'?'0':'1');
            ll zeros = 0, ones = 0;
            rep(i,0,s.size()-1)
            {
                if(s[i]==pre)
                {
                    if(s[i]=='1') zeros++;
                    else ones++;
                }
                pre = s[i];
            }
            cout<<max(zeros,ones)<<endl;
        }
        return 0;
    }
    
    

    C. Chef Monocarp

    题意:有n个菜,每个菜有个最适时刻t[i],每个时刻只能出一道菜。在一个时刻j出第i道菜的贡献是(|t[i] - j|), 现在要你安排出菜时间使得贡献和最小。

    思路:这道题比后面的D题难想一点。。。比赛没想出来,赛后发现是一个dp的问题。
    用dp[i][j]表示前i道菜,在j时刻出菜的最优解。
    当前这个状态可以由
    1.选当前j这个位置,让前i-1道菜从前j-1个位置里面选,然后加上选这个位置的贡献。 (- > dp[i-1][j-1] + |t[i] - j|)
    2.不选当前j位置,直接从j-1位置转移过来,表示利用前j-1个位置出前i道菜。(->dp[i][j-1])
    然后取两个状态的最小值转移过来。
    最后注意一下要先对t数组排序再进行dp。

    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} };
    
    ll dp[500][500];
    ll a[maxn];
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read();
            rep(i,1,n) a[i] = read();
            sort(a+1,a+1+n);
            rep(i,0,n) rep(j,0,2*n) dp[i][j] = inf;
            dp[0][0] = 0;
            rep(i,1,n*2) dp[0][i] = 0;
            rep(i,1,n) rep(j,1,2*n) dp[i][j] = min(dp[i-1][j-1]+abs(a[i] - j), dp[i][j-1]);
            cout<<dp[n][2*n]<<endl;
        }
        return 0;
    }
    
    

    D. Minimal Height Tree

    题意: 给你一个BFS的遍历顺序,且遍历时是按照子节点从小到大排序好访问的。现在问你最小深度是多少。

    思路:这个题比前面的好想很多。
    贪心:
    1.首先我们定义一段连续上升的子串为一个块。因为贪心,如果能放,我们就直接把整个块里面的元素塞到一个父节点下面。
    2.从上面可以发现这样会使得每一层的子节点(这一层所有块里面数量的和)尽量的多,而一个子节点又可以连接一个块,所以就每次拿到一个块,看看这一层还有没有位置可以放,可以的话就塞进去,同时这一层的承载量又可以加上当前块的内部个数。若放不下,就到新的一层继续连,同时更新承载量。

    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} };
    
    ll a[maxn];
    vector<ll> block;
    
    int main()
    {
        int kase;
        cin>>kase;
        while(kase--)
        {
            ll n = read();
            block.clear();
            rep(i,1,n) a[i] = read();
            ll len = 0;
            a[1] = 1;
            rep(i,2,n)
            {
                 if(a[i] > a[i-1]) len++;
                 else block.pb(len), len = 1;
            }
            block.pb(len);
            ll cur_max = 1;
            ll cur_num = 0;
            ll nxt_max = 0;
            int p = 0;
            ll ans = 0;
            while(p<block.size())
            {
                cur_num++;
                if(cur_num > cur_max)
                {
                    cur_max = nxt_max;
                    nxt_max = block[p];
                    cur_num = 1;
                    ans++;
                }
                else nxt_max += block[p];
                p++;
            }
            cout<<ans+1<<endl;
        }
        return 0;
    }
    
    

  • 相关阅读:
    StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(二)小车端
    StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(一)准备工作
    公司名称后缀 Inc. Co.,Ltd.
    Linux C定时器使用指南
    配置QQ企业邮箱小结
    常用PHP文件操作函数
    Git基本命令 -- 基本工作流程 + 文件相关操作
    Git基本命令 -- 创建Git项目
    Entity Framework Core 2.0 入门
    用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传
  • 原文地址:https://www.cnblogs.com/Bgwithcode/p/13891052.html
Copyright © 2020-2023  润新知