Codeforces Round 876 Div2 A-D题解

发布时间 2023-06-11 17:09:37作者: tiany7

Codeforces Round 876 Div2 A-D题解

A.The Good Array

这个题就是问你对于 \(i \leq n\),要求前面后面至少
\(ceil(\frac{i}{k})\) 个 1
那我们就贪心的每k个放一个1,或者直接用数学算一下就好了

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}

int n;
int a[limit];
void solve(){
    int k;
    cin>>n>>k;
    int ans = (n - 1) / k + ((n - 1) % k != 0);
    cout<<ans+1<<endl;
};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
        invoke(solve);
     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

B. Lamps

这道题是问给n个路灯,每个路灯都有一个a和一个b,然后一开始路灯都是关的,每次我们可以打开一盏路灯,获得b个金币,但是如果我们打开了x盏路灯,所有\(a \leq x\) 的路灯都会自动爆炸,问我们最多获得多少金币

这个题一看就很绕,读题读了一万年。主要理解难度在于我想了半天为什么答案是这样,后来发现爆炸的路灯都不算进“开的路灯”里面去,原来如此。注意到a给的小于n,所以应该是可以开桶统计的。然后直觉上我们知道,我们应该先从a小的路灯开启,a一样小的,肯定优先开大的。对于路灯a = i,我们有i盏路灯可以开。就一个模拟,哎,傻逼

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}

int n;
int a[limit];
int b[limit];
int vis[limit];
void solve(){
    cin>>n;
    ll ans = 0;
    map<int, multiset<ll, greater<>>>mp;
    rep(i, 1, n){
        vis[i] = 0;
        cin>>a[i]>>b[i];
        mp[a[i]].insert(b[i]);
        vis[i] = 0;
    }
    rep(i,1,n){
        for(auto &it : mp[i] | views::take(i)){
            ans += it;
        }
    }
    cout<<ans<<endl;





};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
        invoke(solve);
     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

C. Insert Zero and Invert Prefix

这个题给一个01串b,然后每次你能选择一个0插入当前的串的任意位置,但是所有插入位置之前的串都会反转,问能否从一个空串变成b。

这个题好啊,这种题我们不能从乱加的角度想,否则一定会想不出来。然后我们首先观察一下,发现最后一位如果是1,那么肯定变不出来。然后我们如果从尾到头拼接串,就可以保证后面的不受影响,然后前面的,对于0,我们直接加入,对于1,我们把连续的1当作0先塞进去,然后在最后一个1进入的时候放到最后反转前面。这样就可以解决了,一个栈模拟罢了

AC 代码
    #include <bits/stdc++.h>

    using namespace std;
    constexpr int limit =  (4e5  + 5);//防止溢出
    #define INF 0x3f3f3f3f
    #define inf 0x3f3f3f3f3f3f3f
    #define lowbit(i) i&(-i)//一步两步
    #define EPS 1e-9
    #define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
    #define pi(a, b) pair<a,b>
    #define rep(i, a, b) for(ll i = a; i <= b ; ++i)
    #define per(i, a, b) for(ll i = b ; i >= a  ; --i)
    #define MOD 998244353
    #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
    #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
    #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
    typedef long long ll;
    typedef unsigned long long ull;
    char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
    inline ll read() {
    #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
        ll sign = 1, x = 0;
        char s = getchar();
        while (s > '9' || s < '0') {
            if (s == '-')sign = -1;
            s = getchar();
        }
        while (s >= '0' && s <= '9') {
            x = (x << 3) + (x << 1) + s - '0';
            s = getchar();
        }
        return x * sign;
    #undef getchar
    }//快读
    void print(ll x) {
        if (x / 10) print(x / 10);
        *O++ = x % 10 + '0';
    }

    void write(ll x, char c = 't') {
        if (x < 0)putchar('-'), x = -x;
        print(x);
        if (!isalpha(c))*O++ = c;
        fwrite(obuf, O - obuf, 1, stdout);
        O = obuf;
    }

    int n;
    int a[limit];
    int b[limit];
    int vis[limit];
    void solve(){
        cin>>n;
        rep(i,1,n){
            cin>>a[i];
        }
        if(a[n]){
            cout<<"NO"<<endl;
            return;
        }
        deque<int>ans;
        ans.push_back(0);
        int cnt = 0;
        per(i,1,n - 1){
            cnt = a[i] ? cnt + 1 : 0;
            //首先要看是不是截止1
            if(i == 1 and a[i]){
                ans.push_back(cnt);
            }else if (a[i] and !a[i - 1]){
                ans.push_back(cnt);
            }else{
                ans.push_back(0); //继续填0
            }
        }
        cout<<"YES"<<endl;
        for(const auto &it : ans){
            cout<<it<<" ";
        }
        cout<<endl;
    };
    int32_t main() {
    #ifdef LOCAL
        FOPEN;
    //    FOUT;
    #endif
        FASTIO
        int kase;
        cin>>kase;
        while (kase--)
            invoke(solve);
         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
        return 0;
    }

D.Ball Sorting

这个比赛的时候真没想出来正解,赛后看别人题解写的,其实思路很接近了,就是没有想到可以建立逻辑节点为0的情况的状态和如何处理这个问题,wtcl

这个题是给出一个permutation,然后我们有\(k \in [1, n]\)个0号球,可以放在任何位置,但一放置就不可以移动,我们可以花一个金币把和0号球相邻的元素插入任何一个位置,问当我们有\(k \in [1, n]\)个0号球,最少需要多少金币。

首先我们这个问题我们可以看样例,其实有点让我想到一个面试题,但是那个是问partition排序之后让整个数组有序,最少分几个partition的。那么这个问题理解就是如何排序。

首先我们如果观察发现,一个partition内部的插入一个0号球,那么排序的代价一定是\(n - abs({最大连续上升子段})\)的数量,这个很容易想,因为我们可以把所有元素只放到一边。然后我们可以先统计。

然后我们再考虑所有的partition和k如何统计,那么如果我们当前在a[i]的地方,然后切了k刀,那么可以表示为状态\(dp[i][j]\)。那么我们要sort,我们就可以用同样的思维拼sort好的段,当我们找到一个\(a[k] \leq a[i]\)的地方,那么我们就用这个段的拼上切j - 1刀的段 + 1。转移方程如下

if a[i - 1] < a[i], \(dp[i][j] = dp[i - 1][j] + 1\)

if a[k] <= a[i], $dp[i][j] = max(self, dp[k][j - 1] + 1) $

然后我们在统计的时候先统计dp[n][i]的最小值,然后再统计在j处切了i-1段(假设后面的都要操作)的最大值,然后用n减去最大值就是答案。当然需要记得,题目要求的是最多放i个,没有说一定要放i个,所以要记得和放i - 1个对比一下!!

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}

int n;
int a[limit];
int dp[505][505]; //代表前i个用了j个球结尾的最小值
int ans[limit];
void solve(){
    cin>>n;
    rep(i, 1, n){
        cin>>a[i];
    }
    if(is_sorted(a + 1, a + 1 + n)){
        for(int _ : views::iota(0, n)){
            cout<<0<<" ";
        }
        cout<<endl;
        return;
    }
    int maxx = n - 1;
    for_each(dp, dp + n + 1, [](int (&a)[505]){fill(a, a + 505, numeric_limits<int>::min());});
//    dp[0][0] = 0; //前0个用了0个球
//    rep(i, 1, n){
//        rep(j, 0, i) {
//            if(a[i - 1] < a[i]){
//                dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1);
//            }
//        }
//    }
    dp[0][0] = 0;
    fill(ans, ans + n + 1, numeric_limits<int>::min());
    rep(i, 1, n){
        rep(j, 0, i){
            if(a[i - 1] < a[i]){
                dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1);
            }
            rep(k, 0, i - 2){
                if(a[k] < a[i] and j > 0) {
                    dp[i][j] = max(dp[i][j], dp[k][j - 1] + 1);
                }
            }
        }
    }

    rep(i, 1, n){ //统共分为i段,然后前j个的最大不用动的数量
        ans[i] = max(ans[i], dp[n][i]);
        rep(j, 1,n - 1) {
            ans[i] = max(ans[i], dp[j][i - 1]);
        }
    }
    rep(i,1,n){
        ans[i] = max(ans[i], ans[i - 1]);
        cout<<n - ans[i]<<" ";
    }
    cout<<endl;




};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
        invoke(solve);
     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

这个确实参考了别人的代码,我只能说赛时写出来很困难

哎,多练吧,马上又要秋招了,