1. 程式人生 > >Beautiful Now

Beautiful Now

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

這份程式碼是參考後發現的最容易理解且最簡單的程式碼,思路明確。

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int Hash[20];
int vis[20]; 
int n,k;
bool check() //檢查交換次數
{
	fill(vis,vis+n,0);
	int sw=0;
	for(int i=0;i<n;i++)
	{
		if(vis[i])continue;
		int x=0;
		while(vis[i]==0)
		{
			x++;
			vis[i]=1;
			i=Hash[i];
		}
		sw+=x-1;
		if(sw>k)
		return false;//為了不超時這裡不符合條件的要提前退出
	}
	return true;
}
int main()
{
	char s[20];
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s%d",s,&k);
		n=strlen(s);
		
		for(int i=0;i<n;i++)
		{
			Hash[i]=i;
		}
	
		int Min=1e9+7;
		int Max=0;
		do{
			if(s[Hash[0]]!='0'&&check())//不能有前導0,且交換次數等於k; 
			{	
				int sum=0;
				for(int i=0;i<n;i++)
				{
					sum=sum*10+s[Hash[i]]-'0';
				}
				if(sum<Min)
				{
					Min=sum;
				}
				if(sum>Max)
				{
					Max=sum;
				}
			}
		}while(next_permutation(Hash,Hash+n));//全排列
		printf("%d %d\n",Min,Max);
	}
 } 

相關推薦

Beautiful Now 杭電第五場 暴力枚舉

ron case bsp 最小 highlight one lag clu like Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot

【2018多校Beautiful Now HDU - 6351】【dfs+剪枝】

【連結】 http://acm.hdu.edu.cn/showproblem.php?pid=6351 【題意】 大意就是給你一個數n,求在最多k次得交換下,能夠得到的最大的數和最小得數是多少,且數不能有前導0 【分析】 數的大小不超過1e9,也就是9位數字。 顯然地,n個數

HDU 6351 Beautiful Now 全排列,預處理,暴力

題意:十進位制數n.操作:可以交換n的任意兩個digit的位置.  T<=100.1<=n,k<=1e9. 問k次操作後,能得到最小和最大的數字? 貪心:假如要大,當前數字和後面最大數字交換.假如有多種 不一定選最後面的..有反例.. n只有9位數(1e9情況只有

hdu Beautiful Now

                                     Beautiful Now Ti

【2018多校Beautiful Now HDU

【連結】 【題意】 大意就是給你一個數n,求在最多k次得交換下,能夠得到的最大的數和最小得數是多少,且數不能有前導0 【分析】 數的大小不超過1e9,也就是9位數字。 顯然地,n個數至多交換n-1次能變成有序。 然後dfs+剪枝,有序地交換兩個數. 【程式碼

HDU 6351 Beautiful Now DFS+剪枝

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 o

HDU 6351 Beautiful Now(DFS)

Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 750    Accepted

HDU 6351 Beautiful Now(暴力/搜尋)

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

【next_permutation暴力+剪枝】2018 hdu多校第五場 1002 Beautiful Now

Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 626    Accepted

6351 Beautiful Now(杭電多校第5場1002)

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 o

hdu6351 Beautiful Now 全排列+剪枝(暴力) 2018杭電第五場B題

題意:給你一個不超過10^9的數n,和一個k; 有一種操作方式交換這個數的某一位與另一位進行交換 比如 201 可以換成 102,讓你進行k次操作,求出交換後最大的數字和最小的數字. 要點:1 . 某一位的數字可以和它本身進行交換            2 .交換的數字

hdu6351 多校第5場 Beautiful Now

將當前位與後面最小值交換為最優,但可能最小值有多個,比如 2311 交換兩次 最小值為  1123 ,那麼交換哪一個呢,我們dfs遍歷一下所有最小值,然後求一個min,如果當前位是最小值,那麼就不交換,由於最多交換9次所以複雜度不高。 求最大值同理 注意首位不能為0,所以將

HDU 6351 Beautiful Now(全排列列舉)

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 o

HDU6351-Beautiful Now

DFS #include<bits/stdc++.h> using namespace std; string n,minans,maxans; int k,len; void dfs1

HDU 6351 Beautiful Now (並不是貪心)

題目連結 給出一個數,允許最多k次將某對位置的兩個數字交換,問可生成的最小和最大的數是多少。 此題,貪心的解法是個假解法。。。。舉個栗子,k=2時的970979,貪心的求出最大值是999077,但實際上可以達到的最大值是999770。所以這題必然不是個貪心。。。 最長只有

2018hdu杭電多校第五場 hdu6351 Beautiful Now(暴力全排列)

Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1677    Accepted

Beautiful Now

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

2018 hdu多校第五場 1002 Beautiful Now

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 o

hdu6351 Beautiful Now (全排列+循環節)

spa continue bsp min 循環節 ++ 處理 efi 博客 題目傳送門 題意: 給你n和k,你每次能交換n的兩個位,問最多k次後的最小和最大值 思路: 考慮到n到1e9,所以可以用全排列來暴力,但是我們不能全排列之前的數位, 因為n中的

Codeforces 55D Beautiful numbers(數位dp)

pac urn etc number div clu 能夠 是我 tdi   題目大意:T(<=10)組數據,求[a,b]能夠被其每個數位的數都整除的數(a,b<=9*10^18)   這題差一點就想出來了,可是最後一步好難想也好妙啊   首先這個數能夠整除各個