CodeForces-2022-ACM·ICPC-N. Number Reduction

题目背景

2022-2023 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Preferably Teams)

题目链接

https://codeforces.com/contest/1765/problem/N

题目描述

Code1

// VsCode C++模板 | (●'◡'●)
#include 

#include 
using namespace std;
typedef long long LL;
string st;
int k, t;
int main() {
    ios::sync_with_stdio(false);
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> st;
        cin >> k;
        for (int i = 0; i < k; i++) {
            string nowMin = "";
            for (int j = 0; j < st.length(); j++) {
                string nowSt = st.substr(0, j) + st.substr(j + 1, st.length() - j - 1);
                if (nowSt[0] == '0') continue;
                if (nowMin == "" || nowMin > nowSt) nowMin = nowSt;
            }
            st = nowMin;
        }
        cout << st << endl;
    }
    return 0;
}
// WA
// 1
// 45012//无法跳出局部最优解
// 4

// AC
// 1

Code2

// VsCode C++模板 | (●'◡'●)
#include 

#include 
using namespace std;
typedef long long LL;
string st;
int t, k, stlen, stk;
int main() {
    ios::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        cin >> st;
        cin >> k;
        string ans = "";
        stlen = st.length(), stk = k;
        for (int i = 0; i < stlen - stk; i++) {
            int findJ = -1;
            for (int j = 0; j <= k; j++) {
                if (i == 0 && st[j] == '0') continue;
                if (findJ == -1 || st[findJ] > st[j]) findJ = j;
            }
            ans += st[findJ];
            st = st.substr(findJ + 1);
            k -= findJ;
        }
        cout << ans << endl;
    }
    return 0;
}

Code3

// VsCode C++模板 | (●'◡'●)
#include 

#include 
using namespace std;
typedef long long LL;
string st;
int t, k, stlen, stk;
typedef pair<char, int> PP;  //<数字,坐标>
//线段树优化查询
#define MaxN 100000 * 5
int treeLen;
PP treeNode[MaxN * 4 + 2];
void buildTree(int p, int l, int r) {  //[l,r]
    if (l == r) {
        treeNode[p] = {st[l - 1], l - 1};
        return;
    }
    int mid = l + (r - l) / 2;
    buildTree(p * 2, l, mid);
    buildTree(p * 2 + 1, mid + 1, r);
    treeNode[p] = min(treeNode[p * 2], treeNode[p * 2 + 1]);
}
PP getTreeMin(int l, int r, int p, int pl, int pr) {  //[l,r]
    if (l <= pl && pr <= r) return treeNode[p];
    if (pl == pr) {
        if (pl <= r && l <= pl)
            return treeNode[l];
        else
            return {'9' + 1, 10};
    }
    if (l > pr || r < pl) return {'9' + 1, 10};
    PP res = {'9' + 1, 10};
    int mid = pl + (pr - pl) / 2;
    res = min(getTreeMin(l, r, p * 2, pl, mid), getTreeMin(l, r, p * 2 + 1, mid + 1, pr));
    return res;
}
void InitTree(int len) {
    treeLen = len;
    if (len != 0)
        buildTree(1, 1, len);
}
int getMinJPos(int l, int r) {  //[l,r)
    return getTreeMin(l + 1, r, 1, 1, treeLen).second;
}
int main() {
    ios::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        cin >> st;
        cin >> k;
        string ans = "";
        stlen = st.length(), stk = k;
        //特判开头的不能为0
        int findJ = -1;
        for (int j = 0; j <= k; j++) {
            if (st[j] == '0') continue;
            if (findJ == -1 || st[findJ] > st[j]) findJ = j;
        }
        ans += st[findJ];
        st = st.substr(findJ + 1);
        k -= findJ;
        //后面使用LogN进行查询
        stlen = st.length(), stk = k;
        int stL = 0;
        InitTree(stlen);
        for (int i = 0; i < stlen - stk; i++) {
            // auto findJ = min_element(&st[stL], &st[stL + k + 1]) - &st[0];
            auto findJ2 = getMinJPos(stL, stL + k + 1);
            ans += st[findJ2];
            k -= findJ2 - stL;
            stL = findJ2 + 1;
        }
        cout << ans << endl;
    }
    return 0;
}

Others

#include 
#include 
using namespace std;
void solve() {
    string s;
    cin >> s;
    int k;
    cin >> k;
    char ch = *min_element(s.begin(), s.end());
    int l = 0, r = k;
    k = s.size() - k;
    string ans;
    while (k--) {
        char mn = '9';
        for (int i = l; i <= r && mn != ch; i++)
            if (s[i] < mn && !(ans.empty() && s[i] == '0'))
                mn = s[i], l = i + 1;
        ans += mn, ++r;
    }
    cout << ans << endl;
}
int main() {
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

你可能感兴趣的:(题解,信息学笔记,笔记+日常,算法,c++,开发语言)