codeforces 435B

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

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

输入

输入一行,包含两个整数 a 和 k (1 ≤ a ≤ 10^18; 0 ≤ k ≤ 100)。

输出

打印 Pasha 在执行不超过 k 次交换的情况下,所能得到的最大数。

示例

输入
1990 1
输出
9190

输入
300 0
输出
300

输入
1034 2
输出
3104

输入
9090000078001234 6
输出
9907000008001234

 

纯暴力,利用swap交换

#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 10;
char a[maxn];
int k;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> a >> k;
	if (k == 0) {
		cout << a << endl;
		return 0;
	}
	int n = strlen(a);
	int pos;
	for (int i = 0; i < n - 1; i++) {
		pos = i;
		for (int j = i + 1; j < n&&j - i <= k; j++) {
			if (a[j] > a[pos])
				pos = j;	//在k步范围内记录下最大数的位置
		}
			for (int j = pos; j > i; j--) {
				swap(a[j], a[j - 1]);//把最大的数从后向前交换
			}
			k -= (pos - i);//减去步数
	}
	cout << a << endl;
	return 0;
}
		pos = i;
		for (int j = i + 1; j < n&&j - i <= k; j++) {
			if (a[j] > a[pos])
				pos = j;
		}
			for (int j = pos; j > i; j--) {
				swap(a[j], a[j - 1]);
			}
			k -= (pos - i);
	}
	cout << a << endl;
	return 0;
}

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值