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. partition show for oracle version

    Dear all You may remember that my blog has a view script for checking sqlserver partition tables. No ...

  2. 最简单的windows 10 软路由

    因为轻信了 小米路由器3潘多拉固件刷机教程 年前把自己的小米路由器3pro 刷程砖了,然后自己有一台 i5256 的三众小主机,连在电信光猫上,可以拨号,勉强可以用,but 家里的设备那么多尤其手机笔 ...

  3. Go语言实现:【剑指offer】字符流中第一个不重复的字符

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是 ...

  4. Python socket 基础(Client) - Foundations of Python Socket

    Python socket 基础- Foundations of Python Socket 建立socket - establish socket import socket s = socket. ...

  5. 推荐算法之因子分解机(FM)

    在这篇文章我们将介绍因式分解机模型(FM),为行文方便后文均以FM表示.FM模型结合了支持向量机与因子分解模型的优点,并且能够用了回归.二分类以及排序任务,速度快,是推荐算法中召回与排序的利器.FM算 ...

  6. 论文《Entity Linking with Effective Acronym Expansion, Instance Selection and Topic Modeling》

    Entity Linking with Effective Acronym Expansion, Instance Selection and Topic Modeling 一.主要贡献 1. pro ...

  7. django后台处理前端上传和显示图片

      1:项目根目录存放图片的目录 2:settings.py  添加 MEDIA_ROOT = os.path.join(BASE_DIR, "media") 3:url.py 添 ...

  8. Nginx安装(yum源)

    CentOS7 $ vi /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/c ...

  9. iOS开发基础--C语言简述(一)

    先占个坑,回来再补 需要的运行环境,自行搜寻,工具不止一种,不详细叙述. C语言是一门非常重要的编程语言,与硬件底层直接相关,很多语言到最后的接口封装都会选择C语言,因而C语言一直很受欢迎,也务必掌握 ...

  10. C语言低级I/O(UNIX接口)

    头文件说明 以下各函数均在<unistd.h>中 flags的各个值定义于<fcntl.h>中 BUFSIZ定义于<stdlib.h>中 (似乎<stdio. ...