1. 程式人生 > 實用技巧 >Educational Codeforces Round 97 ABCD 題解

Educational Codeforces Round 97 ABCD 題解

A. Marketing Scheme

題意:對於一個x,如果\({\lfloor{x\over 2}\rfloor}\)<= \(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 '\n'
#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 '\n'
#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 '\n'
#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 '\n'
#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;
}