Anton has a positive integer nn, however, it quite looks like a mess, so he wants to make it beautiful after kk swaps of digits. 
Let the decimal representation of nn as (x1x2⋯xm)10(x1x2⋯xm)10 satisfying that 1≤x1≤91≤x1≤9, 0≤xi≤90≤xi≤9 (2≤i≤m)(2≤i≤m), which means n=∑mi=1xi10m−in=∑i=1mxi10m−i. In each swap, Anton can select two digits xixi and xjxj (1≤i≤j≤m)(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 kk 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≤1001≤T≤100, 1≤n,k≤1091≤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

题意
给一个数 N 每次可以交换两个数字的位置
文交换 K 次后 可以得到的最大值和最小值

解法
  应该老老实实开数组作各位分离写数据结构,
  但是脑子抽了筋偏要用算术运算的方法来实现交换位。
  我下次再也不瞎鸡乱写了。

  大概思路
  从高位开始向低位扫描,
  比较当前位与最大/最小值,
  如果有必要交换的话,
  每次把数中合法的(首位不为零)最大/最小数提到当前位置。

错误代码(至今不知道wa在哪里,也不想改了)
  1 #include <bits/stdc++.h>
2 #include <iostream>
3 #include <cstring>
4 #include <stack>
5 #include <cstdlib>
6 #include <queue>
7 #include <cmath>
8 #include <cstdio>
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12 #include <list>
13 #include <iterator>
14 #include <set>
15 #include <map>
16 #include <utility>
17 #include <iomanip>
18 #include <ctime>
19 #include <sstream>
20 #include <bitset>
21 #include <deque>
22 #include <limits>
23 #include <numeric>
24 #include <functional>
25 #include <ctime>
26
27 #define gc getchar()
28 #define mem(a) memset(a,0,sizeof(a))
29 #define mod 1000000007
30 #define sort(a,n,int) sort(a,a+n,less<int>())
31 #define fread() freopen("in.in","r",stdin)
32 #define fwrite() freopen("out.out","w",stdout)
33 using namespace std;
34
35 typedef long long ll;
36 typedef char ch;
37 typedef double db;
38
39 int gcd(int a,int b){
40 if(a<b)swap(a,b);
41 if(a%b==0)return b;
42 else gcd(b,a%b);
43 }
44
45 double random(double start,double end)
46 {
47 return start + (end - start) * rand() / (RAND_MAX + 1.0);
48 }
49 const int maxn = 100+10;
50 const int INF = 0x3f3f3f3f;
51 double dis(double x1,double y1){
52 return sqrt(x1 * x1 + y1 * y1);
53 }
54
55 int main()
56 {
57 ll t = 0;
58 cin >> t;
59 while(t--)
60 {
61 unsigned long long a = 0 , k = 0;
62 unsigned long long res = 0;
63 cin >> a >> k;
64 //cout<<t<<endl;a = t;k = 2;
65 res = a;
66 unsigned long long a1 = a;
67 int j = 0;
68 for(int i = 0 , counter = 0;counter<k;)
69 {
70 unsigned long long s = a;
71 int min = 9;
72 int p = -1;
73 for(j = 0;s/10 != 0;j++)s/=10;
74 //cout<<j<<endl; //
75 if(j==i)break;
76 if(j==1)
77 {
78 if(a%10<a/10 && a%10!=0)
79 res = (a%10) * 10 + a/10;
80 else res = a;
81 break;
82 }
83 s = a;
84 for(int l = 0;l<j-i;l++)
85 {
86 if(s % 10 < min)
87 {
88 if(i!=0 || s % 10!=0)
89 {
90 min = s % 10;
91 p = l;
92 }
93 }
94 s /= 10;
95 }
96 if(p==-1)
97 {
98 res = a;
99 break;
100 }
101 //cout<<i<<' '<<counter<<endl;//
102 //cout<<p<<endl; //
103 s = a;
104 int m = 0,n = 0;
105 int k = 0;
106
107 for(k = 0;k <=j-i;k++)
108 {
109 if(k == p)
110 {
111 m = s % 10;
112 }
113 if(k == j-i)
114 {
115 n = s % 10;
116 }
117 s/=10;
118 }
119 //cout<<m<<' '<<n<<endl; //
120 if(n <= m)
121 {
122 i++;
123 continue;
124 }
125 int mul = 1;
126 for(k = 0;k <=j-i;k++)
127 {
128 if(k == p)
129 {
130 a = a- m*mul +n*mul;
131 }
132 if(k == j-i)
133 {
134 a = a- n*mul +m*mul;
135 }
136 mul*=10;
137 }
138 res = a;
139 //cout<<a<<endl; //
140 i++;
141 counter++;
142 }
143 cout<<res<<' ';
144 a = a1;
145 res = a;
146 for(int i = 0 , counter = 0;counter<k;)
147 {
148 if(i>j)
149 {
150 res = a;
151 break;
152 }
153 ll s = a;
154 int max = 0;
155 int p = 0;
156 for(j = 0;s/10 != 0;j++)s/=10;
157 //cout<<j<<endl; //
158 if(j==i)break;
159 if(j==1)
160 {
161 if(a%10>a/10 && a%10!=0)
162 res = (a%10)*10+a/10;
163 else res = a;
164 break;
165 }
166
167 s = a;
168 for(int l = 0;l<j-i;l++)
169 {
170 if(s % 10 > max)
171 {
172 max = s % 10;
173 p = l;
174 }
175 s /= 10;
176 }
177 //cout<<p<<endl; //
178 s = a;
179 int m = 0,n = 0;
180 int k = 0;
181 //cout<<s<<endl; //
182 for(k = 0;k <=j-i;k++)
183 {
184 if(k == p)
185 {
186 m = s % 10;
187 }
188 if(k == j-i)
189 {
190 n = s % 10;
191 }
192 s/=10;
193 }
194 //cout<<m<<' '<<n<<endl; //
195 if(n >= m)
196 {
197 i++;
198 continue;
199 }
200 int mul = 1;
201 for(k = 0;k <=j-i;k++)
202 {
203 if(k == p)
204 {
205 a = a- m*mul +n*mul;
206 }
207 if(k == j-i)
208 {
209 a = a- n*mul +m*mul;
210 }
211 mul*=10;
212 }
213 res = a;
214 i++;
215 counter++;
216 }
217 cout<<res<<endl;
218
219 }
220 return 0;
221 }


B - Beautiful Now HDU - 6351的更多相关文章

  1. HDU - 6351 Beautiful Now

    Beautiful Now HDU - 6351 Anton has a positive integer n, however, it quite looks like a mess, so he ...

  2. HDU 6351.Beautiful Now-暴力、全排列、思维 (2018 Multi-University Training Contest 5 1002)

    2018 Multi-University Training Contest 5 6351.Beautiful Now 题意就是交换任意两个数字,问你交换k次之后的最小值和最大值. 官方题解: 哇塞, ...

  3. HDU 6351 Beautiful Now(DFS)多校题解

    思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...

  4. HDU 6351 (Beautiful Now) 2018 Multi-University Training Contest 5

    题意:给定数N(1<=N<=1e9),k(1<=k<=1e9),求对N的任意两位数交换至多k次能得到的最小与最大的数,每一次交换之后不能出现前导零. 因为N最多只有10位,且给 ...

  5. 【hdu 6351】Beautiful Now

    [链接] 我是链接,点我呀:) [题意] 你可以最多交换k次数字. 让你组成一个最大的和一个最小的数字. [题解] 直接写个bfs.求出所有状态的最小交换次数. 但是最大值和最小值分开写. 做最大值的 ...

  6. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  7. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

  8. HDU 6351 (带技巧的暴力)

    题意:给定一个数,和一个最多交换次数k,问在不超过k次操作的情况,问可以得到的最大值和最小值是多少? 个人解题的艰辛路程 , 开始是想到了暴力枚举的可能 , 打出来发现在判断枚举的数组与原来数组交换了 ...

  9. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  10. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

随机推荐

  1. 在linux下QImage和QPixmap的内存泄漏和QPixmap线程安全问题

    在linux下QImage和QPixmap的内存泄漏和QPixmap线程安全问题 为什么不在线程里面使用QPixmap(path),而是使用QPixmap::fromImage(QImage(path ...

  2. TINYINT[M]、INT[M]和BIGINT[M]中M值的意义

    TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsign ...

  3. 异常分析 JedisConnectionException: java.net.SocketTimeoutException: Read timed out

    问题描述   测试Redis分布式锁的时候,如果一次执行大量数据,系统会报出如下异常: JedisConnectionException: java.net.SocketTimeoutExceptio ...

  4. 代码随想录第11天 | 二叉树part01

      理论基础 需要了解 二叉树的种类,存储方式,遍历方式 以及二叉树的定义 文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7 ...

  5. CentOS7.* 查询开机启动项

    使用 systemctl list-unit-files 可以查看启动项 左边是服务名称,右边是状态,enabled是开机启动,disabled是开机不启动 过滤查询可以systemctl list- ...

  6. IFREAD's Web 的出版说明

    关于lijnf.top的发布说明 一名高中生开发的个人Web IFREAD's Web 前言 由于主播是高中生,所以肯定不可能每天都用来开发网站,主播计划每隔1-2个月进行一次较大的更新,不定期对小功 ...

  7. 搭建一个图片变视频的AI(一):模型介绍

    在AI如火如荼发展的今天,让图片变视频的功能已经相当成熟了,很多AI软件都可以实现:上传一张图片,一段描述,就可以输出一个视频,实现了让一张图片动起来的效果. 今天教大家这么在自己的电脑上搭建一个:让 ...

  8. 总结下参与以及看到的一些好的业务设计的 pattern

    B端C端进行分离: 单场景业务应用表:业务表进行分离 对于B端系统来说,如发钱系统,B端需要存储 订单id.是否发放成功.通知状态等信息,有可能还会有发放失败,审核驳回等无用数据记录,但是对于C端用户 ...

  9. 如何让外汇WebSocket连接不断线?

    在量化交易系统或行情订阅程序中,WebSocket 是实现实时行情获取的关键通道.但在实际部署中,我们经常会遇到一个头痛的问题:WebSocket连接在运行一段时间后断开了,而我们的策略还以为数据一直 ...

  10. springboot~入门第三篇~与mybatis整合~(未完)

    第二部分仅仅是从控制器到页面的跳转,但是没数据库的整合是不行的. 进入正题: springboot启动是要默认加载数据源的,之前是从application.properties,现在开始在  src/ ...