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. Codeforces_462_B

    http://codeforces.com/problemset/problem/462/B 简单的贪心,排序即可看出来. #include<cstdio> #include<ios ...

  2. ARTS Week 10

    Dec 30, 2019 ~ Jan 5, 2020 Algorithm Problem 88 Merge Sorted Array (合并两个有序数组) 题目链接 题目描述:给定两个有序数组 num ...

  3. [C/C++]const限定符总结

    const限定符 const是一种限定符,被const所限定的变量其值不可以被改变. const的初始化 由于const一旦创建其值就不能够被改变,所以我们必须对其进行初始化 const int a; ...

  4. 详解c++中对二维数组下标[][]的重载

    首先定义一个矩阵类,我用一个二维数组存储矩阵中的数据,矩阵详细定义如下 class Matrix { public: Matrix(int rows, int cols) { _rows = rows ...

  5. 接口测试:http状态码

    http状态码 每发出一个http请求之后,都会有一个响应,http本身会有一个状态码,来标示这个请求是否成功,常见的状态码有以下几种:1.200 2开头的都表示这个请求发送成功,最常见的就是200, ...

  6. .NET Core之单元测试(四):Fluent Assertions的使用

    目录 什么是Fluent Assertions 待测试API 测试用例 什么是Fluent Assertions Fluent Assertions 是 .NET 平台下的一组扩展方法,用于单元测试中 ...

  7. Go语言基础之接口(面向对象编程下)

    1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...

  8. JavaScript Math方法的基本使用

    1.Math.sin()方法 定义:返回一个数的正弦. 语法:Math.sin(x),x必须是一个数值. 实例: <!DOCTYPE html> <html lang="e ...

  9. pikachu-服务器端请求伪造SSRF(Server-Side Request Forgery)

    一.SSRF概述(部分内容来自pikachu平台) SSRF(Server-Side Request Forgery:服务器端请求伪造),其形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功 ...

  10. Python3标准库:itertools迭代器函数

    1. itertools迭代器函数 itertools包括一组用于处理序列数据集的函数.这个模块提供的函数是受函数式编程语言(如Clojure.Haskell.APL和SML)中类似特性的启发.其目的 ...