Pasha Maximizes

发布时间 2023-06-12 14:04:19作者: o-Sakurajimamai-o

题目描述

Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at most k swaps.

输入格式

The single line contains two integers a and (1<=1018; 0<=100)(1<=a<=10e18; 0<=k<=100) .

输出格式

Print the maximum number that Pasha can get if he makes at most k swaps.

题意翻译

有一个长度为n的正整数。可以对这个数进行交换相邻两个数位的操作。最多能进行k次这样的操作,问能得到的最大的数是多少。

输入输出样例

输入 #1
1990 1
输出 #1
9190
输入 #2
300 0
输出 #2
300
输入 #3
1034 2
输出 #3
3104
输入 #4
9090000078001234 6
输出 #4
9907000008001234
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string s;
int res,n,num,cnt,i;
int main()
{
    cin>>s>>n;
    num=n;
    while(num>0){
        while(s[cnt]=='9'&&cnt<=s.size()) ++cnt;
        int maxx=cnt;
        if(cnt>=s.size()) break;
        for(i=cnt+1;i<=num+cnt&&i<s.size();i++) if(s[maxx]<s[i]) maxx=i;
        if(maxx==cnt){//如果后面没有比它大的了,那就找下一位
            cnt++;
            continue;
        }
        for(i=cnt+1;i<=maxx;i++) swap(s[cnt],s[i]),num--;//每一步都要交换
    }
    cout<<s<<endl;
    return 0;
}