Beautiful Now

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

Problem Description
Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps of digits.
Let the decimal representation of n as (x1x2⋯xm)10 satisfying that 1≤x1≤9, 0≤xi≤9 (2≤i≤m), which means n=∑mi=1xi10m−i. In each swap, Anton can select two digits xi and xj (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 k swaps?
 
Input
The first line contains one integer T, indicating the number of test cases.
Each of the following T lines describes a test case and contains two space-separated integers n and k.
1≤T≤100, 1≤n,k≤109.
 
Output
For 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
枚举每种交换情况,分别取最大和去最小,记得特判前导零
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e2+10;
const ll mod = 998244353;
const double pi = acos(-1.0);
ll n, m;
bool cmp( char p, char q ) {
return p > q;
}
string strmin, strmax, s, tmin, tmax;
void dfs1( ll x, ll cnt, string t ) {
if( cnt > n-1 || x == t.length()-1 ) {
//debug(cnt), debug(tmin), debug(strmin), debug(t);
strmin = min(strmin,t);
return ;
}
if( t[x] == tmin[x] ) {
dfs1(x+1,cnt,t);
return ;
}
char c = 'a';
for( ll i = x+1; i < t.length(); i ++ ) {
if( t[i] <= c ) {
if( x == 0 && t[i] == '0' ) {
continue;
}
c = t[i];
}
}
if( c == 'a' ) {
dfs1(x+1,cnt,t);
return ;
}
for( ll i = x+1; i < t.length(); i ++ ) {
if( t[i] == c ) {
swap(t[i],t[x]);
dfs1(x+1,cnt+1,t);
swap(t[i],t[x]);
}
}
}
void dfs2( ll x, ll cnt, string t ) {
if( cnt > n-1 || x == t.length()-1 ) {
//debug(cnt), debug(tmax), debug(strmax), debug(t);
strmax = max(strmax,t);
return ;
}
if( t[x] == tmax[x] ) {
dfs2(x+1,cnt,t);
return ;
}
char c = '0';
bool flag = true;
for( ll i = x+1; i < t.length(); i ++ ) {
if( t[i] >= c ) {
c = t[i];
flag = false;
}
}
for( ll i = x+1; i < t.length(); i ++ ) {
if( t[i] == c ) {
swap(t[i],t[x]);
dfs2(x+1,cnt+1,t);
swap(t[i],t[x]);
}
}
}
string rev( string s ) {
string t = "";
for( ll i = 0, j = s.length()-1; i < s.length(); i ++, j -- ) {
t = t + s[j];
}
return t;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll T, t = 1;
cin >> T;
while( T -- ) {
s = "";
ll j = 0;
cin >> m >> n;
while(m) {
char c = (m%10)+'0';
s = s + c;
m /= 10;
}
s = rev(s);
tmin = s, tmax = s;
sort(tmin.begin(),tmin.end());
sort(tmax.begin(),tmax.end(),cmp);
if( tmin[0] == '0' ) {
char c = 'a';
ll inx = -1;
for( ll i = 1; i < tmin.length(); i ++ ) {
if( tmin[i] != '0' && tmin[i] < c ) {
c = tmin[i];
inx = i;
}
}
if( inx != -1 ) {
swap(tmin[inx],tmin[0]);
}
}
if( n >= s.length()-1 ) {
cout << tmin << " " << tmax << endl;
} else {
strmin = s;
dfs1(0,0,strmin);
strmax = s;
dfs2(0,0,strmax);
cout << strmin << " " << strmax << endl;
}
}
return 0;
}
/*
123112 2
111322 322111
10001 2
*/

  

hdu6351 Beautiful Now 杭电第五场 暴力枚举的更多相关文章

  1. hdu6354 杭电第五场 Everything Has Changed 计算几何

    Everything Has Changed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  2. hdu6373 Pinball 杭电第六场 物理知识

    Pinball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  3. 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  4. 杭电第六场 hdu6362 oval-and-rectangle 积分求期望

    oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...

  6. hdu 1290_献给杭电五十周年校庆的礼物

    Description 或许你曾经牢骚满腹或许你依然心怀忧伤或许你近在咫尺或许你我天各一方 对于每一个学子母校 永远航行在生命的海洋 今年是我们杭电建校五十周年,这是一个值得祝福的日子.我们该送给母校 ...

  7. HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)

    传送门: 献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. [HDU1290]献给杭电五十周年校庆的礼物

    [HDU1290]献给杭电五十周年校庆的礼物 题目大意: 问\(n(n\le1000)\)个平面能够将一个三维空间分成几部分. 思路: 公式\(\frac{n^3+5n+6}6\). 源代码: #in ...

  9. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

随机推荐

  1. Mac相关快捷键操作

    拷贝: shift + option + 拖动拖动至目的地 创建快捷方式: option + command + 拖动至目的地

  2. ImageView 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  3. Docker:跨主机通信

    修改主机docker默认的虚拟网段,然后在各自主机上分别把对方的docker网段加入到路由表中,配合iptables即可实现docker容器夸主机通信.配置方法如下: 设有三台虚拟机 v1: 10.1 ...

  4. Android buildType混淆代码

    [话题引入] ①在Android开发完成,我们会将代码打包成APK文件.选择 菜单栏 Build --> Build APK ②将查看视图切换到 Project 模式,文件夹下有一个debug模 ...

  5. 算法与数据结构基础 - 合并查找(Union Find)

    Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...

  6. [趣学程序]java的常用类之String

    java基础之常用类 String类 String表示字符串,所谓字符串,就是一连串的字符,是java中最常用的类之一. String是不可变类,一旦String对象被创建,包含在对象中的字符序列(内 ...

  7. Sql Or NoSql,看完这一篇你就懂了

    前言 你是否在为系统的数据库来一波大流量就几乎打满CPU,日常CPU居高不下烦恼?你是否在各种NoSql间纠结不定,到底该选用那种最好?今天的你就是昨天的我,这也是写这篇文章的初衷. 这篇文章是我好几 ...

  8. SQL中一些实用的快捷键

    Ctrl+A全选 快速选中一行: 若光标在这条语句末尾用Shift+Home 若光标在这条语句开头用Shift+End Ctrl+K+U快捷注释本行 Ctrl+K+C反注释 Ctrl+R  关闭下面的 ...

  9. Codeforces 436D Pudding Monsters

    题意简述 开始有无限长的一段格子,有n个格子种有布丁怪兽,一开始连续的布丁怪兽算一个布丁怪兽. 每回合你可以将一个布丁怪兽向左或右移动,他会在碰到第一个布丁怪兽时停下,并与其合并. 有m个特殊格子,询 ...

  10. PKI机制总结

    PKI,全称是Public Key Infrastructure,可译为公钥基础设施.它是因特网中节点通信的安全保障机制,HTTPS中的‘S’就来源于PKI. 要去学习一个技术,首先要从它的源头考虑— ...