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?

InputThe first line contains one integer TT, indicating the number of test cases. 
Each of the following TT lines describes a test case and contains two space-separated integers nn and kk. 
1≤T≤1001≤T≤100, 1≤n,k≤1091≤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
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int maxx,minn,k,len;
int c[],sum1[],sum2[],p[];
char ss[]; void update()
{
if(c[ p[] ]==)
{
return;
}
// 用sum1记录当前排列
for(int i=;i<=len;++i)
{
sum1[i]=p[i];
} int kk=,s=;
for(int i=;i<=len;++i)
{
s=s* + c[ p[i] ];
if(sum1[i] != i)
{
for(int j=i+;j<=len;++j)
{
if(sum1[j]==i)
{
swap(sum1[i],sum1[j]);
++ kk;
// 这一序列不能在k步内实现
if(kk>k)
{
return;
}
break;
}
}
}
} // 当前队列满足条件
// 更新最大最小值
maxx=max(maxx,s);
minn=min(minn,s);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(sum1,,sizeof(sum1));
memset(sum2,,sizeof(sum2));
scanf("%s %d",ss,&k); len=strlen(ss); for(int i=;i<len;++i)
{
// 字符转数字
c[i+]=ss[i]-'';
++ sum1[ c[i+] ];
++ sum2[ c[i+] ];
} // 剪枝
if(k>=len-)
{
// 输出最小数
// 输出第一位(特判非零)
for(int i=;i<=;++i)
{
if(sum1[i])
{
printf("%d",i);
-- sum1[i];
break;
}
} for(int i=;i<=;++i)
{
while(sum1[i])
{
printf("%d",i);
--sum1[i];
}
} printf(" "); // 输出最大数
for(int i=;i>=;--i)
{
while(sum2[i])
{
printf("%d",i);
-- sum2[i];
}
}
printf("\n");
continue;
} // 未能剪枝 // 找一个互不相等的排列(从小到大有序)
// 以初始排列为当前数字的编号
for(int i=;i<=len;++i)
{
p[i]=i;
}
// 初始化最大最小值
minn=2e9;
maxx=-;
// 自当前排列开始更新满足条件时的最大最小值
do
{
update();
}while(next_permutation(p+,p+len+)); // 需要头文件algor
// 九位数的全排列,有362880种
// 最多尝试次数,为九次
// 复杂度约为 10^7
printf("%d %d\n",minn,maxx);
}
return ;
}
												

C - Beautiful Now的更多相关文章

  1. 使用Beautiful Soup编写一个爬虫 系列随笔汇总

    这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...

  2. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(1): 基础知识Beautiful Soup

    开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful ...

  3. Python爬虫学习(11):Beautiful Soup的使用

    之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...

  4. 推荐一些python Beautiful Soup学习网址

    前言:这几天忙着写分析报告,实在没精力去研究django,虽然抽时间去看了几遍中文文档,还是等实际实践后写几篇操作文章吧! 正文:以下是本人前段时间学习bs4库找的一些网址,在学习的可以参考下,有点多 ...

  5. 数位DP CF 55D Beautiful numbers

    题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...

  6. 错误 You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work

    Win  10    下python3.6 使用Beautiful Soup  4错误 You are trying to run the Python 2 version of Beautiful ...

  7. hihoCoder 1425 : What a Beautiful Lake(美丽滴湖)

    hihoCoder #1425 : What a Beautiful Lake(美丽滴湖) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...

  8. Python学习笔记之Beautiful Soup

    如何在Python3.x中使用Beautiful Soup 1.BeautifulSoup中文文档:http://www.crummy.com/software/BeautifulSoup/bs3/d ...

  9. Python Beautiful Soup学习之HTML标签补全功能

    Beautiful Soup是一个非常流行的Python模块.该模块可以解析网页,并提供定位内容的便捷接口. 使用下面两个命令安装: pip install beautifulsoup4 或者 sud ...

  10. 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)

    4692: Beautiful Spacing Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 46  Solved: 21[Submit][Statu ...

随机推荐

  1. 机器学习总结-bias–variance tradeoff

    bias–variance tradeoff 通过机器学习,我们可以从历史数据学到一个\(f\),使得对新的数据\(x\),可以利用学到的\(f\)得到输出值\(f(x)\).设我们不知道的真实的\( ...

  2. POJ_1485_dp

    题目描述: 每组数据给n个点,点按一维坐标升序给出,要求划分成k块,在每一块中,取一个站,要求每个块中所有的点到站的距离的和的总和最小. 思路: dp题,dp[i][j]表示i个点分成j块的最小距离, ...

  3. java4选择结构 二

    public class jh_01_为什么使用switch选择结构 { /* * 韩嫣参加计算机编程大赛 * 如果获得第一名,将参加麻省理工大学组织的1个月夏令营 * 如果获得第二名,将奖励惠普笔记 ...

  4. overflow:hidden;zoom:1;外框自适应 [转]

    在排页面时,碰到了,外框里的元素用fluid 布局,外框的高度不能适应的问题,查了一下资料,发现了博友的一篇文章,解决了这个问题,现在分享给大家. 解释不到位的请大牛补充~~~~~~ 高度自适应: h ...

  5. 【题解】P1020 导弹拦截

    [题解]P1020 导弹拦截 从n^2到nlogn 第二问就是贪心,不多说 第一问: 简化题意:求最长不下降子序列 普通n^2: for (int i = 1; i <= n; i++) for ...

  6. 2020牛客寒假算法基础集训营4 D:子段异或

    D : 子段异或 考察点 : 位运算,前缀和,异或的性质和应用 坑点 : 0 - L 的异或值是 0 的话也是一个区间 相同的值可能有多个,那么这时候区间就会有多个(x * (x + 1) / 2) ...

  7. 【OpenGL】OpenGL4.3常用指令目录

    参考OpenGL编程指南 第8版 VAO void glGenVertexArrays(GLsizei n, GLuint *arrays); 返回n个未使用的对象名到数组arrays中,用作顶点数组 ...

  8. 02_TypeScript数据类型

    typescript中为了使编写的代码更规范,更有利于维护,增加了类型校验,写ts代码必须指定类型.   1.布尔类型(boolean) var flag:boolean = true;   2.数字 ...

  9. sun.misc.Unsafe中一些常用方法记录

    sun.misc.Unsafe中一些常用方法记录 前情摘要 sun公司提供了可以用于直接操作内存的类,这个类就是sun.misc.Unsafe.因为Java本身是不会涉及到直接操作内存的,Java A ...

  10. [Linux]LVM扩展卷

    LVM LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活 ...