Codeforces Round #555 (Div. 3) B - Long Number 贪心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/89738828

为了保证最优,从左边开始遍历,把第一个能交换的开始交换连续的一段

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;

const int maxn = 10 + 7;

int n;
string s;
int a[maxn];

void solve(int id) {
    for(int i = id; i < n; ++i) {
        int t = (s[i]-'0');
        if(a[t] >= t) {
            s[i] = (a[t] + '0');
        }
        else return;
    }
}

int main() {
    cin >> n;
    cin >> s;
    for(int i = 1; i <= 9; ++i) {
        cin >> a[i];
    }
    for(int i = 0; i < n; ++i) {
        int t = (s[i]-'0');
        if(a[t] > t) {
            solve(i);
            break;
        }
    }
    cout << s;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/89738828