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

B. Long Number
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a long decimal number a consisting of n digits from 1 to 9. You also have a function f that maps every digit from 1 to 9 to some (possibly the same) digit from 1 to 9.

You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x). For example, if a=1337, f(1)=1, f(3)=5, f(7)=3, and you choose the segment consisting of three rightmost digits, you get 1553 as the result.

What is the maximum possible number you can obtain applying this operation no more than once?

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of digits in a.

The second line contains a string of n characters, denoting the number a. Each character is a decimal digit from 1 to 9.

The third line contains exactly 9 integers f(1), f(2), …, f(9) (1≤f(i)≤9).

Output
Print the maximum number you can get after applying the operation described in the statement no more than once.

Examples
inputCopy
4
1337
1 2 5 4 6 6 3 1 9
outputCopy
1557
inputCopy
5
11111
9 8 7 6 5 4 3 2 1
outputCopy
99999
inputCopy
2
33
1 1 1 1 1 1 1 1 1
outputCopy
33

思路:
这道题是吃死了英语的亏 读题出现失误 首先这道题我们去找出九个f(i)里大于i 的去使用map进行标记 这里直接标记为了f(i)的值 然后就是题意 这道题并不是去替换所有f(i)中大于i的数字 而是找到一个合适段落 替换其中全部的数字 求得到的最大值 这样一想也没有什么难度 只需要从第一个元素开始向后遍历 找到第一个变小的元素就停止遍历 值得注意的是有可能出现在第一个替换后变大的元素前面有可能出现变小的元素 对于这些元素我们不做处理 以局部最优达到全局最优

AC代码:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
char book[200000+10];
int tmp[15];
int m;
int main()
{
    map<int,int> mp;
    cin >> m;
    scanf("%s",book);
    for(int i=1;i<=9;i++)
    {
        cin >> tmp[i];
        if(tmp[i]>i) mp[i]=tmp[i];
        if(tmp[i]==i) mp[i]=10;//标记相等的数字
    }
    int ans=0;
    int flag=0;
    for(int i=0;i<m;i++)
    {
        if(mp[book[i]-'0'])
        {
            if(flag==0) ans=0;  //出现第一个元素变大的元素之前有可能出现使其变小的元素
            if(mp[book[i]-'0']!=10)
            {
                flag++;          //相等不记录
                book[i]=mp[book[i]-'0']+'0';
            }
        }else if(!mp[book[i]-'0'])
        {
            ans++;
        }
        if(flag && ans) break;
    }
    puts(book);
    return 0;
}

原文链接: https://www.cnblogs.com/lizhaolong/p/16437424.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/395885

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月5日 下午1:46
下一篇 2023年4月5日 下午1:46

相关推荐