CF Gym 102114B. Beautiful Now解题报告(搜索+贪心)

题目链接:http://codeforces.com/gym/102114/problem/B
题目描述:
Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps of digits.

Let the decimal representation of n as (x1x2… xm)10 satisfying that 1 ≤ x1 ≤ 9, 0 ≤ xi ≤ 9 (2 ≤ i ≤ m), which means . In each swap, Anton can select two digits xi and xj (1 ≤ i ≤ j ≤ m) and then swap them if the integer after this swap has no leading zero.

Could you please tell him the minimum integer and the maximum integer he can obtain after k swaps?

Input
The first line contains one integer T, indicating the number of test cases.

Each of the following T lines describes a test case and contains two space-separated integers n and k.

1 ≤ T ≤ 100, 1 ≤ n, k ≤ 10^9.

Output
For each test case, print in one line the minimum integer and the maximum integer which are separated by one space.

Example
input
5
12 1
213 2
998244353 1
998244353 2
998244353 3
output
12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332

题目大意:
给你一个整数, 你可以将任意两个位置的数进行交换, 可以进行最多k次, 问你能够达到的最小和最大的数是多少。

题解:
首先发现这个数字只有9位数, 而交换次数可能很多, 我们贪心的话就是如果要最小的每次交换把最小的放前面, 最大的同理, 但是这样在次数够的时候能够保证正确性, 如果次数不够不一定能最优,例如7899,换两次最大是9987但是如果我们按照贪心把后面的最大的放到前面换两次是9978不是最大,那么怎么解决呢?考虑什么情况次数不够, 就是次数小于他的长度时候, 对于大于等于长度是够的, 每次选一个, 最多选len-1次所以只要大于等于len-1就可以换成升序或者降序, 但是要注意前导0的处理, 题目要求结果不能有前导0,那么对于其他情况也就是次数小于len-1那么就可以随便爆搜了, 毕竟最多9位数。

代码:

#include 

typedef long long ll;

const int maxn = 1e3 + 10;

using namespace std;

int t, k, len; 
string s1, s, s2;

void dfs(int dep, int num){
	s2 = min(s2, s);
	if (num == k) return ;
	if (dep == len - 1) return ;
	int minnum = 11, pos = -1;
	for (int i = dep;i < len;i ++){
		if (dep == 0){
			if (s[i] - '0' < minnum && s[i] - '0' != 0){
				minnum = s[i] - '0';
				pos = i;
			}
		}
		else{
			if (s[i] - '0' < minnum){
				minnum = s[i] - '0';
				pos = i;
			}
		}
	}
	int p[12], cnt = 0;
	for (int i = dep+1;i < len;i ++)
		if (s[i] - '0' == minnum) p[++cnt] = i;
	if (minnum == s[dep] - '0'){
		dfs(dep + 1, num);
		return ;
	}
	for (int i = 1;i <= cnt;i ++){
		swap(s[p[i]], s[dep]);
		dfs(dep+1, num+1);
		swap(s[p[i]], s[dep]);
	}
}
void dfs2(int dep, int num){
	s2 = max(s2, s);
	if (num == k) return ;
	if (dep == len - 1) return ;
	int maxnum = -1, pos = -1;
	for (int i = dep;i < len;i ++){
		if (s[i] - '0' > maxnum){
			maxnum = s[i] - '0';
			pos = i;
		}
	}
	int p[12], cnt = 0;
	for (int i = dep+1;i < len;i ++)
		if (s[i] - '0' == maxnum) p[++cnt] = i;
	if (maxnum == s[dep] - '0'){
		dfs2(dep + 1, num);
		return ;
	}
	for (int i = 1;i <= cnt;i ++){
		swap(s[p[i]], s[dep]);
		dfs2(dep+1, num+1);
		swap(s[p[i]], s[dep]);
	}
}
int main(){
	cin >> t;
	while(t --){
		s1.clear();
		cin >> s1 >> k;
		s.clear();
		s2.clear();
		s = s1;
		len = s1.length();
		if (k >= len-1){
			sort(s1.begin(), s1.end());
			if (s1[0] == '0'){
				int pp = -1;
				for (int i = 0;i < len;i ++){
					if (s1[i] != '0'){
						pp = i;
						break;
					}
				}
				if (pp == -1) printf("0");
				else{
					printf("%c", s1[pp]);
					for (int i = 0;i < len;i ++){
						if (i == pp) continue;
						printf("%c", s1[i]);
					}
				}
			}
			else for (int i = 0;i < len;i ++) printf("%c", s1[i]);
			printf(" ");
			for (int i = len-1;i >= 0;i --) printf("%c", s1[i]);
			cout << endl;
			continue;
		}
		s2 = "9999999999";
		dfs(0, 0);
		cout << s2 << " ";
		s2.clear();
		s2 = "0000000000";
		s = s1;
		dfs2(0, 0);
		cout << s2 << endl;
	}
}

你可能感兴趣的:(acm题目总结,搜索,贪心)