2018 Multi-University Training Contest 5 Beautiful Now (Hdu6351 dfs || 全排列)

本篇博客介绍了一道编程题“BeautifulNow”,任务是通过限定次数的数字交换得到一个整数的最大值和最小值。文章提供了两种解题思路:一种是对下标的全排列进行暴力枚举;另一种是使用DFS深度优先搜索来减少不必要的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Beautiful Now

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


 

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

题目大意:

给你一个数,并给你k次交换的机会,并记录交换期间最大和最小值,(求最大值和最小值的时候问题是独立的)

解题思路:

因为数字是小于10的九次方,也就是说如果我们将每一位的数字用数组来存的话最多就只有十个,那么我们就能用对下标全排列来暴力枚举所有情况,最多也就9的阶乘次,所以并不会TLE

全排列代码:

#include<bits/stdc++.h>
using namespace std;
#define MAXN 10
int mark[MAXN];//用于标记有没有访问 
int num[MAXN];//保存每一位的数字 
int index[MAXN];//记录下标 
char str[MAXN];//一开始以字符串的形式输入 
int k,len;
bool check() //检查是否满足条件 
{
	int count=0;
	memset(mark,0,sizeof(mark));
	for(int i=0;i<len;i++){
		if(!mark[i]){
			int temp=-1; //初始化为-1方便计算交换的次数 
			while(!mark[i]){ //如果第二次还没访问过那么就下标改变了,也就是交换了一次 
				mark[i]=1;
				temp++;
				i=index[i];
			}
			count+=temp; 
			if(count>k) //当交换的次数大于所规定的次数,那么对于这种排列情况就不存在 
			return 0;
		}
	}
	return 1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%s %d",&str,&k);
		len=strlen(str);
		int sum=0;
		for(int i=0;i<len;i++){
			num[i]=str[i]-'0'; 
			sum*=10;
			sum+=num[i];
			index[i]=i;
		}
		int ansmin,ansmax; //记录其最大值和最小值 
		ansmin=sum;
		ansmax=sum;
		do{
			if(num[index[0]]!=0 && check()){ //保证不能为前导0 
				int temp=0;
				for(int i=0;i<len;i++){
					temp*=10;
					temp+=num[index[i]];
				}
				temp<ansmin? ansmin=temp:0;
				temp>ansmax? ansmax=temp:0;
			}
		}while(next_permutation(index,index+len));  //对整个数组下标进行全排列 
		printf("%d %d\n",ansmin,ansmax);
	}
}

当然还有一种方法就是dfs,当我们寻找最小情况的时候,在遍历时最小值可能会出现多个,那么可以在遍历的时候进行dfs来确保最小的情况,因为dfs不用枚举所有情况,所以时间复杂度自然比全排列小的多。

dfs代码:

#include<bits/stdc++.h>
using namespace std;
#define MAXN 15
char str[MAXN],ans[MAXN],mo[MAXN];  
int len,k;
void mindfs(int x,int sum) //求最小值的dfs 
{
	if(sum==0 || x>len){ //跳出dfs 
		if(strcmp(ans+1,str+1)>0)  //当ans小于str时将str赋值给ans 
		strcpy(ans+1,str+1);
		return;
	}
	int  min=x;
	for(int i=x;i<=len;i++){ //寻找最小值的下标 
		if(str[min]>str[i])min=i;
	}
	if(min!=x){ //如果当前下标不是最小值进行循环 
		for(int i=x+1;i<=len;i++){
			if(str[i]==str[min]){ //当遇到最小值时进行交换并继续递归 
				swap(str[i],str[x]);
				mindfs(x+1,sum-1);
				swap(str[i],str[x]);
			}
		}
	}
	else //如果是最小值的时候直接从下个下标进行dfs 
	mindfs(x+1,sum);
}
void maxdfs(int x,int sum)//求最大值的dfs 
{
	if(sum==0 || x>len){
		if(strcmp(ans+1,str+1)<0) strcpy(ans+1,str+1);
		return;
	}
	int  max=x;
	for(int i=x;i<=len;i++){
		if(str[max]<str[i])
		max=i;
	}
	if(max!=x){
		for(int i=x+1;i<=len;i++){
			if(str[i]==str[max]){
				swap(str[x],str[i]);
				maxdfs(x+1,sum-1);
				swap(str[x],str[i]);
			}
		}
	}
	else
	maxdfs(x+1,sum);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%s %d",mo+1,&k);
		len=strlen(mo+1);
		strcpy(str+1,mo+1);
		strcpy(ans+1,mo+1);
		int tp=1;
		for(int i=2;i<=len;i++){
			if(str[tp]>str[i] && str[i]!='0')  //不能有前导0 
			tp=i;
		}
		if(tp!=1){  
			for(int i=2;i<=len;i++){
				if(str[i]==str[tp]){ //交换首位 
					swap(str[1],str[i]);
					mindfs(2,k-1);
					swap(str[1],str[i]);
				}
			}
		}
		else{
			mindfs(2,k);
		}
		printf("%s ",ans+1);
		strcpy(str+1,mo+1);
		strcpy(ans+1,mo+1);
		tp=1;
		for(int i=2;i<=len;i++){
			if(str[tp]<str[i] && str[i]!='0')
			tp=i;
		}
		if(tp!=1){
			for(int i=2;i<=len;i++){
				if(str[i]==str[tp]){
					swap(str[1],str[i]);
					maxdfs(2,k-1);
					swap(str[1],str[i]);
				}
			}
		}
		else
		maxdfs(2,k);
		printf("%s\n",ans+1);
	}
}

 

用户表: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id 是该表的主键(具有唯一值的列)。 该表中的每行包括用户 ID 和用户名。 注册表: Register +-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+ (contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 该表中的每行包含用户的 ID 和他们注册的赛事。 编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 返回结果如下示例所示。 示例 1: 输入: Users 表: +---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+ Register 表: +------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+ 输出: +------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 解释: 所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%
03-18
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值