C - Beautiful Now
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的更多相关文章
- 使用Beautiful Soup编写一个爬虫 系列随笔汇总
这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...
- 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(1): 基础知识Beautiful Soup
开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful ...
- Python爬虫学习(11):Beautiful Soup的使用
之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...
- 推荐一些python Beautiful Soup学习网址
前言:这几天忙着写分析报告,实在没精力去研究django,虽然抽时间去看了几遍中文文档,还是等实际实践后写几篇操作文章吧! 正文:以下是本人前段时间学习bs4库找的一些网址,在学习的可以参考下,有点多 ...
- 数位DP CF 55D Beautiful numbers
题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...
- 错误 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 ...
- hihoCoder 1425 : What a Beautiful Lake(美丽滴湖)
hihoCoder #1425 : What a Beautiful Lake(美丽滴湖) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...
- Python学习笔记之Beautiful Soup
如何在Python3.x中使用Beautiful Soup 1.BeautifulSoup中文文档:http://www.crummy.com/software/BeautifulSoup/bs3/d ...
- Python Beautiful Soup学习之HTML标签补全功能
Beautiful Soup是一个非常流行的Python模块.该模块可以解析网页,并提供定位内容的便捷接口. 使用下面两个命令安装: pip install beautifulsoup4 或者 sud ...
- 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)
4692: Beautiful Spacing Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 46 Solved: 21[Submit][Statu ...
随机推荐
- Affinity Propagation
1. 调用方法: AffinityPropagation(damping=0.5, max_iter=200, convergence_iter=15, copy=True, preference=N ...
- Features for Multi-Target Multi-Camera Tracking and Re-identification论文解读
解读一:Features for Multi-Target Multi-Camera Tracking and Re-identification Abstract MTMCT:从多个摄像头采集的视频 ...
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- String、StringBuffer和StringBuilder总结
String String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象. StringBuffer 当对字符串进行修改的时候,需要使用 StringBuf ...
- JAVA体系结构简单介绍
JAVA 体系结构包括四个独立但相关的技术: java程序设计语言 java class 文件格式 JAVA应用编程接口(API) JAVA虚拟机(JVM) 当编写运行一个java程序时,就同时体验了 ...
- Nginx 主要应用场景
前言 本文只针对 Nginx 在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 ...
- Kvm 简介 安装 使用 桥接网络
KVM 全称是 基于内核的虚拟机(Kernel-based Virtual Machine),它是一个 Linux 的一个内核模块,该内核模块使得 Linux 变成了一个 Hypervisor: 它由 ...
- 线段树学习----C语言
/* 线段树学习:如果一个节点为i,那么他的左孩子为2I+1,右孩子为2i+2: */ #include<stdio.h> #define min(a,b) a<b?a:b; ]; ...
- MATLAB添加工具箱及无法连接到MathWorks问题
版本信息:官网下载的MATLAB R2019b 学生版 操作系统:Windows 10 在安装MATLAB时,需要我们自行选择要安装工具箱,如何在已安装MATLAB后添加当初没有选择安装的工具箱呢?第 ...
- 06_TypeScript泛型
1.泛型的定义 泛型就是解决 类,接口 方法的复用性,以及对不特定数据的支持(类型校验). 2.泛型函数 //T 表示泛型,具体什么类型是调用这个方法的时候决定的, //T可以用其他大写字母表示,传入 ...