Codeforces Round 555 (Div. 3)题解

· · 个人记录

A. Reachable Numbers

题目链接

难度:普及-

标签:模拟,数学

题意:给你一个初始的数字x,你可以对它进行任意次操作,每次操作你先对x+1,如果此时x末尾有0你需要把0删除掉,问你进行任意次操作一共可以得到多少种不同的数字。

思路:直接暴力模拟过程即可,一直操作直到遇到相同的数字就停止,每10次操作至少会减少一位,那么操作次数不会超过数字的位数*10。

code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 1e6+10, M = 510;

int n;

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
    cin>>n;
    map<int, int> vis;
    int sum = 0;
    while(1) {
        if(vis[n]) break;
        vis[n] = 1, sum ++;
        n ++;
        while(n%10 == 0) n /= 10;
    }
    cout<<sum<<"\n";
    return 0;
}

B. Long Number

题目链接

难度:普及-

标签:贪心,枚举

题意:给你1-9每个数字的一个映射关系,然后给你长度为n的字符串,你可以修改这个字符串的任意一个子区间,选定子区间所有的字符都要使用这个映射关系,问你可以得到的最大字典序是多少。

思路:从高位向低位贪心,如果当前位映射后变大了就一直调整直到遇到一个变小的。

code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 1e6+10, M = 510;

int n, a[N];
string s;

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
    cin>>n>>s;
    for(int i = 1; i <= 9; i ++) {
        cin>>a[i];
    }
    for(int i = 0; i < n; i ++) {
        int k = s[i]-'0';
        if(a[k] > k) {
            for(int j = i; j < n; j ++) {
                int p = s[j]-'0';
                if(p > a[p]) break;
                s[j] = a[p]+'0';
            }
            break;
        }
    }
    cout<<s<<"\n";
    return 0;
}

C1. Increasing Subsequence (easy version)

题目链接

难度:普及-

标签:贪心

题意:给你一个数组,每次你可以从数组的两端选择一个数字加到我们的选择序列中去并在原数组中删除这个数组,但是我们需要是得到的序列是递增的,同时使得我们得到的序列长度最长,输出最长长度以及删除的操作过程。

思路:由于简单版本保证每个数字不一样那么我们考虑这样直接贪心,优先选两边小的删除,因为大的那个在随后可以删除,这样一定比先删大的优,因为多删了一个小的。重复这个过程直到不能删除为止。

code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 1e6+10, M = 510;

int n, a[N];
string s;

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
    cin>>n;
    for(int i = 1; i <= n; i ++) {
        cin>>a[i];
    }
    int l = 1, r = n, pre = 0;
    string ans;
    while(l <= r) {
        if(max(a[l], a[r]) <= pre) break;
        if(a[r] < pre || (a[l] > pre && a[l] < a[r])) {
            pre = a[l], l ++, ans += 'L';
        }else {
            pre = a[r], r --, ans += 'R';       
        }
    }
    cout<<ans.size()<<"\n";
    cout<<ans<<"\n";
    return 0;
}

C2. Increasing Subsequence (hard version)

题目链接

难度:普及

标签:贪心

题意:和上面一样,只是没了数字不一样的限制。

思路:考虑数字相同的情况会有什么影响,会导致在前面的贪心操作中某次删除会遇到两边数字一样的情况,思考一下其实这个情况我们就只会从某一边一直删到底了,所以枚举判断一下那种删的多就行了。

code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 1e6+10, M = 510;

int n, a[N];
string s;

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
    cin>>n;
    for(int i = 1; i <= n; i ++) {
        cin>>a[i];
    }
    int l = 1, r = n, pre = 0;
    string ans;
    while(l <= r) {
        if(max(a[l], a[r]) <= pre) break;
        if(a[l] == a[r]) {
            int mxl = 1, mxr = 1;
            int now = a[l];
            for(int i = l+1; i <= r; i ++) {
                if(a[i] > now) now = a[i], mxl ++;
                else break;
            }
            now = a[r];
            for(int i = r-1; i >= l; i --) {
                if(a[i] > a[r]) now = a[i], mxr ++;
                else break;
            }
            for(int i = 0; i < max(mxl, mxr); i ++) {
                ans += mxl > mxr ? 'L' : 'R';
            }
            break;
        }else if(a[r] < pre || (a[l] > pre && a[l] < a[r])) {
            pre = a[l], l ++, ans += 'L';
        }else {
            pre = a[r], r --, ans += 'R';       
        }
    }
    cout<<ans.size()<<"\n";
    cout<<ans<<"\n";
    return 0;
}

D. N Problems During K Days

题目链接

难度:普及+/提高

标签:构造,贪心,二分

题意:你需要构造一个长度为k的数组,数组需要满足两个条件:1、元素都要大于0,后一个数要比前一个数大,但不能超过前一个数的两倍。2、数组的元素和为n,存在的话输出YES并输出任意一种构造方案,否则输出NO。

思路:直接先构造一个长度为k的首项为1,公差为1的等差数列,如果此时(k+1)*k/2>n的话那么无法构造,然后给每个元素在加上(n-(k+1)*k/2)/k,最后还剩下(n-(k+1)*k/2)%k个元素,从前往后在满足上述条件的情况下依次加即可。

code:

#include<bits/stdc++.h>
#define pb push_back
typedef long long LL;
using namespace std;
const int N = 1e6+10, M = 510;

LL n, k, a[N];

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
    cin>>n>>k;
    int p = n;
    n -= (1+k)*k/2;
    for(int i = 1; i <= k ;i ++) {
        a[i] = i;
    }
    if(n < 0) cout<<"NO\n";
    else {
        for(int i = 1; i <= k; i ++) a[i] += n/k;
        n %= k;
        for(int i = 2; i <= k; i ++) {
            if(k-i+1 > n) continue;
            if(a[i]+1 <= a[i-1]*2) {
                for(int j = i; j <= k; j ++) {
                    a[j] ++, n --;
                }
            }
        }
        if(n) {
            cout<<"NO\n";return 0;
        }
        for(int i = 2; i <= n; i ++) {
            if(a[i] > a[i-1]*2) {
                cout<<"NO\n";
                return 0;
            }
        }
        int sum = 0;
        cout<<"YES\n";
        for(int i = 1; i <= k; i ++) {
            cout<<a[i]<<" ";
            sum += a[i];
        }
        assert(sum == p);
        cout<<"\n";
    }
    return 0;
}

E. Minimum Array