交换一个数字的任意两个位置,指定K次的最值
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
InputThe 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.
OutputFor 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
int n, k;
char s[50], f[50];
int len;
struct node
{
string str;
int pos, num;
};
queue<node>que; int cal(string str) {
int res = 0;
for(int i = 0; i < len; i++) res = res*10+(str[i]-'0');
return res;
} void bfs1() {
while(!que.empty()) que.pop();
que.push({s+1, 0, 0});
int ans = inf; while(!que.empty()){
node v = que.front(); que.pop();
int p = v.pos;
if (v.num <= k) ans = min(ans, cal(v.str));
if (p >= len) break;
if (v.num > k) continue;
int mmin = 1000;
for(int i = p+1; i < len; i++) {
if (p == 0 && v.str[i] == '0') continue;
mmin = min(mmin, v.str[i]-'0');
}
if (mmin >= (v.str[p]-'0')){
que.push({v.str, p+1, v.num});
continue;
}
int sign = 0;
for(int j = len-1; j > p; j--){
sign = 1;
if ((v.str[j]-'0') == mmin) {
swap(v.str[j], v.str[p]);
que.push({v.str, p+1, v.num+1});
swap(v.str[j], v.str[p]);
}
}
if (!sign) que.push({v.str, p+1, v.num});
}
cout << ans << " ";
} void bfs2(){
//swap()
while(!que.empty()) que.pop();
que.push({s+1, 0, 0});
int ans = -1; while(!que.empty()) {
node v = que.front(); que.pop();
int p = v.pos;
if (v.num <= k) ans = max(ans, cal(v.str));
if (p >= len) break;
if (v.num > k) continue;
int mmax = -1;
for(int i = p+1; i < len; i++) mmax = max(mmax, v.str[i]-'0');
if (mmax <= (v.str[p]-'0')) {
que.push({v.str, p+1, v.num});
continue;
}
int sign = 0;
for(int j = len-1; j > p; j--){
sign = 1;
if ((v.str[j]-'0') == mmax) {
swap(v.str[j], v.str[p]);
que.push({v.str, p+1, v.num+1});
swap(v.str[j], v.str[p]);
}
}
if (!sign) que.push({v.str, p+1, v.num});
}
cout << ans << endl;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t; cin >> t;
while(t--){
scanf("%s%d", s+1, &k);
len = strlen(s+1);
bfs1();
bfs2();
}
return 0;
}
交换一个数字的任意两个位置,指定K次的最值的更多相关文章
- Java 将任意数组的任意两个位置的数据进行交换
package yw.fanxing; /** * 自定义泛型测试 * * 写一个方法,将任意数组的任意两个位置的数据进行交换 * * @author yw-tony * */ public clas ...
- 如果在num1的任何位置有一个数字的连续三倍,并且在num2中有一个数字的连续两倍,则返回1。 如果不是这样,则返回0
''' 它接受数字num1和num2,如果在num1的任何位置有一个数字的连续三倍,并且在num2中有一个数字的连续两倍,则返回1. 如果不是这样,则返回0 例子 triple_double(4519 ...
- 一个数组nums,其中任意两个值等于给定值target,返回这两个值在nums里的位置
package com.java.tencent; import java.lang.reflect.Array; import java.util.Arrays; import java.util. ...
- C语言:一个数组中只有两个数字是出现一次
//1.一个数组中只有两个数字是出现一次, //其他所有数字都出现了两次. //找出这两个数字,编程实现.a //^=单独两个数的^结果 //单独出现的两个数不同位的标记 //position: ^结 ...
- 小易邀请你玩一个数字游戏,小易给你一系列的整数。你们俩使用这些整数玩游戏。每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字。 例如: 如果{2,1,2,7}是你有的一系列数,小易说的数字是11.你可以得到方案2+2+7 = 11.如果顽皮的小易想坑你,他说的数字是6,那么你没有办法拼凑出和为6 现在小易给你n个数,让你找出无法从n个数中选取部分求和
小易邀请你玩一个数字游戏,小易给你一系列的整数.你们俩使用这些整数玩游戏.每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字. 例如: 如果{2,1,2 ...
- 谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做?
谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做? 分析: "假设两个整数数组为A和B,各有N个元素,任意两个数的和组成的数组C有N^2个元素. ...
- 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字(python)(原创)
背景: 电话面试&手撕代码 2019.03.22 Mufasa 问题: 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字 条件: 这串数字是有序数 解决方法: 核心代码只有 ...
- 42.输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的。
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的. 这道题有很多烟雾弹: 首先如果有多对,最前面的两个数就是乘积最小的, ...
- 一个整型数组里除了一个数字之外,其他的数字都出现了两次。要求时间复杂度是O(n),空间复杂度是O(1),如何找出数组中只出现一次的数字
思路分析:任何一个数字异或它自己都等于0,根据这一特性,如果从头到尾依次异或数组中的每一个数字,因为那些出现两次的数字全部在异或中抵消掉了,所以最终的结果刚好是那些只出现一次的数字. 代码如下: #i ...
随机推荐
- 2019-8-2-WPF-从文件加载字体
title author date CreateTime categories WPF 从文件加载字体 lindexi 2019-08-02 17:10:33 +0800 2018-2-13 17:2 ...
- UVA 12563 "Jin Ge Jin Qu hao" (背包)
传送门 debug了好一会,突然发现,输出错了,emmm......... 明天再写debug历程: (PS:ipad debug是真的繁琐) 题意: 题解: 尽管题干中给的 t 的范围很大,但是 t ...
- 剑指 Offer —— 数组中重复的数字
数组中的重复数字 题目描述 牛课网链接 长度为 n 的数组里,所有数字都在 0 到 n-1 的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一 ...
- H3C DNS域名解析完整过程
- H3C 因特网域名结构树
- 2018-2-13-win10-uwp-csdn-博客阅读器
title author date CreateTime categories win10 uwp csdn 博客阅读器 lindexi 2018-2-13 17:23:3 +0800 2018-2- ...
- codeforces 1183F 离散化枚举 约数定理
codeforces1183F 有技巧的暴力 传送门:https://codeforces.com/contest/1183/problem/F 题意: 给你n个数,要你从中选出最多三个数,使得三个数 ...
- codeforces 11D(状压dp)
传送门:https://codeforces.com/problemset/problem/11/D 题意: 求n个点m条边的图里面环的个数 题解: 点的范围只有19,很容易想到是状压. dp[sta ...
- AbstactFactory模式
AbstractFactory模式就是用来解决这类问题的:要创建一组相关或者相互依赖的对象. AbstractFactory模式关键就是将这一组对象的创建封装到一个用于创建对象的类(ConcreteF ...
- ELK学习实验010:Logstash简介
Logstash是具有实时流水线功能的开源数据收集引擎.Logstash可以动态统一来自不同来源的数据,并将数据规范化为您选择的目标.清除所有数据并使其民主化,以用于各种高级下游分析和可视化用例. 虽 ...