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. VC简单操作mysql

    #include <iostream> #include <winsock.h> #include <mysql.h> #pragma comment(lib, & ...

  2. 什么是伪静态,以及ubuntu + apache 如何实现伪静态

    原文链接:http://www.cnblogs.com/ainiaa/archive/2010/07/25/1784564.html php伪静态 一直在做php的开发工作.在开发的过程中老早就听说了 ...

  3. gRPC初识

    RPC算是近些年比较火热的概念了,随着微服务架构的兴起,RPC的应用越来越广泛.本文介绍了RPC和gRPC的相关概念,并且通过详细的代码示例介绍了gRPC的基本使用. RPC是什么 在分布式计算,远程 ...

  4. 简单看看ThreadPoolExecutor原理

    线程池的作用就不多说了,其实就是解决两类问题:一是当执行大量的异步任务时线程池能够提供较好的性能,在不使用线程池时,每当需要执行异步任务是需要直接new一个线程去执行,而线程的创建和销毁是需要花销的, ...

  5. 【WPF学习】第四十四章 图画

    通过上一章的学习,Geometry抽象类表示形状或路径.Drawing抽象类扮演了互补的角色,它表示2D图画(Drawing)——换句话说,它包含了显示矢量图像或位图需要的所有信息. 尽管有几类画图类 ...

  6. docker pull 时报错Create more free space in thin pool or use dm.min_free_space option to change behavior

    docker pull 时报错: failed to register layer: devmapper: Thin Pool has 107394 free data blocks which is ...

  7. day13 JS Dom

    js两种存在形式 1:文件 2:块 放到body标签底部 防止加载js超时页面反应慢的问题 声明变量 name = "sb"; //全局变量 var age=18; //局部变量 ...

  8. HttpMessageNotReadableException

    HttpMessageNotReadableException 情况描述: spring boot web项目,尝试使用热部署工具. Controller只写了用来测试异常的方法, 异常处理器去捕获异 ...

  9. 二次开发的Selenium Demo版本

    文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为Mac上Selenium不支持拖拽,只能这样做了,4个文件--------------------------------------- ...

  10. C语言编写程序的大小端问题

    有时候,用C语言写程序需要知道大端模式还是小端模式,,由于寄存器大于一个字节(8bit),就会存在一个字节安排的问题,例如(16bit)的short型,(32bit)的int型,具体需要看具体的编译器 ...