Codeforces Round 699 & 772题解

发布时间 2023-10-02 07:15:23作者: tiany7

Codeforces Round 699 & 772题解

今天手感确实不错,看来合理的训练配合合理的休息是很重要的。前些日子的努力没白费。不过,怎么说呢,现在的形势不是我把算法题和基础知识做好的就行了,该从系统的角度去作为一个ld去思考问题了,感觉自己还是有点欠缺的,不过我也在积极努力的学习中,希望能够早日成为一个合格的ld。

Round 699

A. Space Navigation

这题就是要删几个命令罢了,没啥难度,傻逼题

B. New Colony

这个题意思是要从i = 1滚石头,然后如果遇到\(h_i \geq h_{i + 1}\) 就继续滚, 否则就停下在当前元素 + 1,如果到了n就直接掉海里输出-1。问最后第m块石头停下的位置在哪里。

这个题很绕,刚开始以为是个模拟,后来发现好像不太对。想了下也就几种情况,首先因为\(a[i] \leq 100\), 所以所有大于100n的结果无一例外肯定是-1。剩下的就模拟着玩就行了。

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (2e5  + 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, m;
int a[limit];

void solve() {
    int ans = 1;
    deque<int>q;
    cin>>n>>m;
    rep(i,1,n){
        int x;
        cin>>a[i];
        q.push_back(a[i]);
    }
    if(m > 100 * n){
        cout<<-1<<endl;
        return;
    }
    rep(i,1,m - 1){
        rep(j, 1, n - 1){
            if(a[j] < a[j + 1]){
                a[j]++;
                break;
            }
        }
    }
    rep(i, 1, n - 1){
        if(a[i] < a[i + 1]){
            cout<<i<<endl;
            return;
        }
    }
    cout<<-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;
}

C. Fence Painting

这个题是给n个fence,m桶油漆,每次油漆可以把a数组的某个元素变成c[i], 每桶油漆必须要用掉,问是否可行,如果可行输出方案,不可行就输出NO。

首先这个题一看就是找后缀,因为最后我们只要知道第i个fence最后变成了b[i],那么我们根本不需要care他之前变成过什么。所以安排也可以按照这个思路来。

如果我们油漆有重复的或者不需要的,那么就把多余的不需要的油漆给一个已经被paint成目标颜色的fence,这样就不会影响最后的结果了。然后paint完之后检查一下是否满足要求,然后输出方案。我这边用了个vector存,先存的是不需要被paint的,然后存必须要paint的,这样从后往前取数可以保证优先供给必须要paint的fence。

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (2e5  + 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, m;
int a[limit];
int b[limit];
vector<int>g[limit];
int c[limit];
void solve() {
    cin>>n>>m;
    rep(i,1,n)g[i].clear();
    rep(i,1,n){
        cin>>a[i];
    }
    rep(i,1,n){
        cin>>b[i];
        if(a[i] == b[i])g[b[i]].push_back(i); //只有不相等的情况才需要改变
    }
    rep(i,1,n){
        if(a[i] != b[i])g[b[i]].push_back(i);
    }
    rep(i,1,m){
        cin>>c[i];
    }
    deque<int>freed;
    deque<int>ans(m + 1, -1);
    int last = -1;
    per(i,1,m){
        int now = c[i];
        if(g[now].size()){
            ans[i] = g[now].back();
            g[now].pop_back();
            last = ans[i];
            a[ans[i]] = c[i];
        }else{
            if(last == -1){
                cout<<"NO"<<endl;
                return;
            }
            ans[i] = last;
        }
    }
    rep(i,1,n){
        if(a[i] - b[i]){
            cout<<"NO"<<endl;
            return;
        }
    }
    cout<<"YES"<<endl;
    for(const auto & it : ans | views::drop(1)){
        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. AB Graph

不会,晚上回家看一下

E. Sorting Books

这个题是给n本数,要把所有相同书放到一起,然后每次可以抽任意一本放到最后面去,问最少几次才能shuffle。

这个题我么不妨这样想,首先在最暴力的情况下,我们至多只需要n - 1次就能把所有的书本给shuffle好。

然后我们不妨把这个问题转化为,有多少书本可以留在这个地方不动,然后我们把每本书出现最早和最后的位置给处理出来,每次dp一下记录当前位置如果都shuffle了,那么最多有多少个位置是不动的,然后用n减掉就是最小需要move的。

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (5e5  + 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, m;
int a[limit];
int l[limit], r[limit];
int dp[limit][2];
void solve() {
    cin>>n;
//    set<int>col;
//    vector<pair<int, int>>p;
    map<int, int>cnt;
    rep(i,1,n){
        cin>>a[i];
    }

    rep(i,1,n){
        r[a[i]] = i;
    }
    per(i,1,n){
        l[a[i]] = i;
        cnt[a[i]]++;
        dp[i][0] = max(dp[i + 1][0], cnt[a[i]]);
    }
    int ans = n - 1;
    rep(i,1,n){
        auto &&self = dp[i][1];
        self = dp[i - 1][1];
        if(r[a[i]] == i){ // rightmost point
            self = max(self, cnt[a[i]] + dp[l[a[i]] - 1][1]);
        }
        int num = dp[i + 1][0] + self;
        ans = min(ans, n - num);
    }
    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;
}

Round 772

A. Min Or Sum

这个题本来以为是啥高难度题,然后一看这不就or起来么,好像老cf经常有这种看上去很难然后写起来极度简单的东西。记得刚打CF第一次做出来C就是有个数学题,直接输出了a[1] < a[n] yes否则no,结果意外地AC了,interesting

B. Avoid Local Maximums

这个题是问最少多少次操作可以消除局部最大值

然后我们发现我们在i位置的时候,只要把i + 1设置成max(a[i], a[i + 2])就能一次最多消除两个,样例把这一点说的很明白。

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (3e5  + 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, m;
int a[limit];
void solve() {
    cin>>n;
    rep(i,1,n){
        cin>>a[i];
    }
    set<int>odd,even;
    rep(i, 2, n - 1){
        if(a[i] > a[i - 1] and a[i] > a[i + 1]){
            auto && now = odd;
            now.insert(i);
        }
    }
    if(odd.empty() and even.empty()){
        cout<<0<<endl;
        rep(i,1,n){
            cout<<a[i]<<" ";
        }
        cout<<endl;
        return;
    }
    a[n + 1] = a[n];
    int ans = 0;
    // 先奇数后偶数
    rep(i,2,n - 1){
        if(a[i] > a[i - 1] and a[i] > a[i + 1]){
            a[i + 1] = max(a[i], a[i + 2]);
            ++ans;
        }
    }

    cout<<ans<<endl;
    rep(i,1,n){
        cout<<a[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;
}

C. Differential Sorting

这个题是要求给出\(1 \leq x < y < z \leq n\),每次把\(a_x = a_y - a_z\),然后把这个array变成sorted的,可以操作n次。

然后我们首先观察题目条件,发现如果最后的两个元素不是sorted,那么我们也没有办法操作他们,所以这种情况一定是NO。

然后接下来就用n - 1和n构造出来前n -2 个就行了。然后再判断下是不是sorted就好。

本来还以为是dp啥的,想到这一点灵光一现,果然是对的

nice

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (3e5  + 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, m;
int a[limit];
void solve() {
    cin>>n;
    multiset<pi(int, int)>s;
    rep(i,1,n){
        cin>>a[i];
        s.insert({a[i], i});
    }
    if(is_sorted(a + 1, a + 1 + n)){
        cout<<0<<endl;
        return;
    }
    if(a[n - 1] > a[n]){
        cout<<-1<<endl;
        return;
    }
    vector<tuple<int, int ,int>>ans;
    ll minn = numeric_limits<ll>::min();
    rep(i,1,n - 2){
        a[i] = a[n - 1] - a[n];
        ans.push_back({i, n - 1, n});
    }
    if(!is_sorted(a + 1, a + 1 + n)){
        cout<<-1<<endl;
        return;
    }
    cout<<ans.size()<<endl;
    for(auto [x,y, z] : ans){
        cout<<x<<" "<<y<<" "<<z<<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. Infinite Set

这个题是给一个set,然后每次可以把set内数字x变成2 * x + 1和4x,问小于\(2^p\)的元素最后有多少个。

这个题是一个思维好题。

首先我们发现,2*x + 1 一定不等于4x(废话),然后既然扯到了二进制我们就从二进制的角度去考虑,那么第一个操作相当于每次左移1位,然后第二个操作属于左移2位。

简单起见我们先从set = {1}开始考虑,那么这个问题实质上就是我们每次可以左移1位或者2位,问小于\(2^p\)的数最后有多少个。

那么这个问题就变成了爬楼梯问题,也就是斐波那契数列,总共的数量我们用前缀和记录一下。

然后我们要dedup一下,因为发现可能会有重复的,那么我们逆向去拆数字,因为我们每次操作要么是奇数,要么是4的倍数,所以我们这样还原分解,如果发现其中的某个中间状态在数组里出现,就丢弃,因为set没有重复元素

然后我们对着dedup之后的数组,每个元素找到msb,然后 \(pref[p - msb + 1]\) 就是这个元素最后的贡献,然后求和就行了。

代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit =  (3e5  + 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;
}
#define int ll
int n, m;
int a[limit];
ll f[limit];
ll pref[limit];
constexpr ll mod = 1e9 + 7;
ll get_highest_bit(ll x){
    ll res = 0;
    while(x){
        x >>= 1;
        ++res;
    }
    return res;
}

void solve() {
    ll ans = 0;
    cin>>n;
    int p;
    cin>>p;
    set<int>s;
    rep(i,1,n){
        cin>>a[i];
        s.insert(a[i]);
    }
    f[1] = 1;
    f[2] = 1;
    pref[1] = 1;
    pref[2] = f[1] + f[2];
    rep(i,3,2e5){
        f[i] = (f[i - 1] + f[i - 2]) % mod;
        pref[i] = (pref[i - 1] + f[i]) % mod;
    }
    vector<int>dedup;

    rep(i,1,n){
        ll now = a[i];
        bool is_dup = false;
        while(now){
            if(now bitand 1){
                now -= 1;
                now >>= 1;
            }else if(now % 4 == 0){
                now /= 4;
            }else{
                break;
            }
            if(s.contains(now)){
                is_dup = true;
                break;
            }
        }
        if(not is_dup){
            dedup.push_back(a[i]);
        }
    }
    for(const auto &num : dedup){
        ll msb = get_highest_bit(num);
        if(p >= msb){
            ans += pref[p - msb + 1];
//            cout<<p - msb<<" "<<pref[p - msb]<<endl;
            ans %= mod;
        }
    }
    ans = (ans + mod) % mod;
    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;
}
    

还得学习下分布式的知识,加油