描述 Description
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.
输入格式 InputFormat
The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).
输出格式 OutputFormat
Print the maximum number that Pasha can get if he makes at most k swaps.
样例输入 SampleInput
39940894417248510 10
样例输出 SampleOutput
99984304417248510
代码 Code
最暴力的贪心即可。每次从当前位置找当前能移动且有意义的最大值移到当前位置。
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define inf 0x7fffffff/27.11
int i,j,t,n,m,l,r,k,z,y,x;
char a[20];
int main()
{
scanf("%s %d",a+1,&k);
m=1;
while (k>0)
{
if (m>strlen(a+1)) break;
t=0;x=0;
for (i=m;i<=m+k;i++)
{
if (i>strlen(a+1)) continue;
if (t<a[i]-'0')
{
t=a[i]-'0';
x=i;
}
}
if (t==a[m]-'0')
{
m++;
continue;
}
for (i=x;i>m;i--) a[i]=a[i-1];
a[m]=t+'0';
k-=(x-m);
m++;
}
printf("%s",a+1);
return 0;
}