B - Beautiful Now HDU - 6351
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的更多相关文章
- HDU - 6351 Beautiful Now
Beautiful Now HDU - 6351 Anton has a positive integer n, however, it quite looks like a mess, so he ...
- HDU 6351.Beautiful Now-暴力、全排列、思维 (2018 Multi-University Training Contest 5 1002)
2018 Multi-University Training Contest 5 6351.Beautiful Now 题意就是交换任意两个数字,问你交换k次之后的最小值和最大值. 官方题解: 哇塞, ...
- HDU 6351 Beautiful Now(DFS)多校题解
思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...
- HDU 6351 (Beautiful Now) 2018 Multi-University Training Contest 5
题意:给定数N(1<=N<=1e9),k(1<=k<=1e9),求对N的任意两位数交换至多k次能得到的最小与最大的数,每一次交换之后不能出现前导零. 因为N最多只有10位,且给 ...
- 【hdu 6351】Beautiful Now
[链接] 我是链接,点我呀:) [题意] 你可以最多交换k次数字. 让你组成一个最大的和一个最小的数字. [题解] 直接写个bfs.求出所有状态的最小交换次数. 但是最大值和最小值分开写. 做最大值的 ...
- HDU 6351暴力枚举 6354计算几何
Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- HDU 6351 Naive Operations(线段树)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...
- HDU 6351 (带技巧的暴力)
题意:给定一个数,和一个最多交换次数k,问在不超过k次操作的情况,问可以得到的最大值和最小值是多少? 个人解题的艰辛路程 , 开始是想到了暴力枚举的可能 , 打出来发现在判断枚举的数组与原来数组交换了 ...
- 搜索入门_简单搜索bfs dfs大杂烩
dfs题大杂烩 棋盘问题 POJ - 1321 和经典的八皇后问题一样. 给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
随机推荐
- 第9.1讲、Tiny Encoder Transformer:极简文本分类与注意力可视化实战
Tiny Encoder Transformer:极简文本分类与注意力可视化实战 项目简介 本项目实现了一个极简版的 Transformer Encoder 文本分类器,并通过 Streamlit 提 ...
- 20244104陈思淼 《Python程序设计》实验三报告
课程:<Python程序设计> 班级:2441 姓名:陈思淼 学号:20244104 实验教师:王志强 实验日期:20254月20日 必修/选修: 公选课 1.实验内容 创建服务端和客户端 ...
- Golang遍历空数组实现指定次数的循环
var nums [10][0]int for range nums { fmt.Println("这里循环输出十次") } golang 的for遍历还是比较简单的,为什么还要用 ...
- Filezilla提权漏洞复现
FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能.可控性.有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP客户 ...
- HTML常见的标签---表单
1 <table> 2 <tr> 3 <!-- name 一定要带上以后交互 --> 4 <td>用户名:</td> 5 <td> ...
- Ceph性能测试
一.测试分类 ceph的性能测试包含rados性能测试和rbd性能测试 二.测试工具 rados性能测试工具:使用ceph自带的rados bench工具.rados losd-gen工具 rbd性能 ...
- linux环境下的redis安装
选择一个安装目录 cd /usr/src/redis/ 下载redis,下载路径的版本号可以改 wget https://download.redis.io/releases/redis-3.2.0. ...
- ChunJun 1.16 Release版本即将发布,bug 捉虫活动邀您参与!
亲爱的社区小伙伴们,历时数月,我们很高兴地宣布,ChunJun 即将迎来 1.16 Release 版本的正式发布.在新版本中,ChunJun 新增了一批常用功能,进行了多项功能优化和问题修复,并在用 ...
- hot100之数组
最大子数组和(053) 先看代码 class Solution { public int maxSubArray(int[] nums) { int n = nums.length; int subS ...
- [abc313 h/ex] Group Photo
Ex - Group Photo 很牛的题 设\(A_0=A_{n+1}=INF\),那么对于每个\(B_i\)有\(B_i>\min(A_{i-1},A_i)\),所以考虑设\(C_i\)表示 ...