题目:

Pasha 有一个正整数 a,不含前导 0 。今天,他认为这个数太小,希望把它变得更大一些。不幸的是,Pasha 只能交换这个整数的两个相邻的十进制数字。

请帮助 Pasha 计算出,他在不超过 k 次交换的条件下,能够取得的最大数是多少。

题解:贪心,一个多位数要想最大,那一定时越靠近前面得数越大越好(废话),而这一题数最大才1e18,也就是长度不超过20的一个字符串,显然我们可以直接暴力:从做左向右开始从当前位置暴力枚举向后扫描,看在满足操作次数的前提下能够放在当前位置最大的数是谁,然后进行交换操作,依次类推。

AC代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
char s[maxn];
int main() {
//string s;
int x,cnt=0;
cin>>s+1;
cin>>x;
int n=strlen(s+1);
for(int i=1;i<=n;i++){
int temp=i;
cnt=0;
for(int j=i+1;j<=n;j++){
if(s[j]>s[temp]&&x>=j-i){
temp=j;
cnt=j-i;
}
}
if(s[temp]==s[i])continue;
for(int k=temp;k>i;k--){
swap(s[k],s[k-1]);
}
x-=cnt;
}
cout<<s+1<<endl;
}