2018 Multi-University Training Contest 5--Beautiful Now--hdu--6351

Beautiful Now

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1635    Accepted Submission(s): 603

Problem Description

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 n=∑mi=1xi10m−i. 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≤109.

Output

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

Sample Input

5

12 1

213 2

998244353 1

998244353 2

998244353 3

Sample Output

12 21

123 321

298944353 998544323

238944359 998544332

233944859 998544332

Source

2018 Multi-University Training Contest 5

题意:

 给你一个数字N,然后你可以交换任意每一位数字k次,输出交换后的最大最小值。

题解:

因为可以自己跟自己交换,所以只要K大于当前数字长度位数len-1,就直接输出最优解即可。

其他情况下,列出当前数组全排列情况并按照交换步数划分,选出当前步数最小最大值即可。

注意首位不能是0。

#include <bits/stdc++.h>
using namespace std;
char a[20];
int c[20],q[20],q1[20],p[20],k,len;
int minn,maxx;

void update() {
	if(c[p[1]]==0) return; //第一位数字是0,不符合规则
	for(int i=1; i<=len; i++) q[i]=p[i]; //q数组内保存全排列后的位置
	int k1=0,s=0;
	for(int i=1; i<=len; i++) {
		s=s*10+c[p[i]];
		if(q[i]!=i) { //如果当前位数字更改了
			for(int j=i+1; j<=len; j++) {
				if(q[j]==i) {
					swap(q[i],q[j]);
					k1++;
					if(k1>k) return;
					break;
				}
			}
		}
	}
	if(k1>k) return;//转换了超过k次,就不行,可以的话,再看
	maxx=max(maxx,s);
	minn=min(minn,s);
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		memset(q,0,sizeof(q));
		memset(q1,0,sizeof(q1));
		scanf("%s%d",a+1,&k);
		len=strlen(a+1);
		for(int i=1; i<=len; i++) {
			c[i]=a[i]-'0';
			q[c[i]]++;
			q1[c[i]]++;
		}
		if(k>=len-1) { //直接得到最大最小!!!不加等于就TLE
			for(int i=1; i<=9; i++) {
				if(q[i]) {
					printf("%d",i);
					q[i]--;
					break;
				}
			}
			for(int i=0; i<=9; i++) {
				while(q[i]) {
					printf("%d",i);
					q[i]--;
				}
			}
			printf(" ");
			for(int i=9; i>=0; i--) {
				while(q1[i]) {
					printf("%d",i);
					q1[i]--;
				}
			}
			printf("\n");
			continue;	
		}
		for(int i=1; i<=len; i++) p[i]=i; //每一位数字原来保存在哪里
		minn=2e9,maxx=-1;
		do {
			update();
		} while(next_permutation(p+1,p+len+1)) ; //全排列
		printf("%d %d\n",minn,maxx);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值