hdu6351 Beautiful Now 杭电第五场 暴力枚举
Beautiful Now
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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?
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.
12 1
213 2
998244353 1
998244353 2
998244353 3
123 321
298944353 998544323
238944359 998544332
233944859 998544332
#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 杭电第五场 暴力枚举的更多相关文章
- hdu6354 杭电第五场 Everything Has Changed 计算几何
Everything Has Changed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- hdu6373 Pinball 杭电第六场 物理知识
Pinball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- 杭电第六场 hdu6362 oval-and-rectangle 积分求期望
oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...
- hdu 1290_献给杭电五十周年校庆的礼物
Description 或许你曾经牢骚满腹或许你依然心怀忧伤或许你近在咫尺或许你我天各一方 对于每一个学子母校 永远航行在生命的海洋 今年是我们杭电建校五十周年,这是一个值得祝福的日子.我们该送给母校 ...
- HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)
传送门: 献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [HDU1290]献给杭电五十周年校庆的礼物
[HDU1290]献给杭电五十周年校庆的礼物 题目大意: 问\(n(n\le1000)\)个平面能够将一个三维空间分成几部分. 思路: 公式\(\frac{n^3+5n+6}6\). 源代码: #in ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
随机推荐
- 【Python-Django模型迁移】用户数据库模型的迁移(对其他数据库迁移同样适用)!!!
迁移用户模型类 1. 指定用户模型类 文档 思考:为什么Django默认用户模型类是User? 阅读源代码:'django.conf.global_settings’ AUTH_USER_MODEL ...
- [nghttp2]压测工具,源码编译并进行deb打包过程
编译环境:deepin 15.11桌面版 nghttp2下载地址:https://github.com/nghttp2/nghttp2 环境要求 emm只能在类Linux环境才能完整编译,想在Wind ...
- koa2基于stream(流)进行文件上传和下载
阅读目录 一:上传文件(包括单个文件或多个文件上传) 二:下载文件 回到顶部 一:上传文件(包括单个文件或多个文件上传) 在之前一篇文章,我们了解到nodejs中的流的概念,也了解到了使用流的优点,具 ...
- dns自动配置shell脚本
代码: #!/bin/bash #获取url echo "url:" read url #获取ip echo "ip:" read ip #向/etc/name ...
- Json串与实体的相互转换 (不依赖于jar包 只需Eclipse环境即可)
Json串与实体的相互转换 (不依赖于jar包 只需Eclipse环境即可) 最近学习了javaWeb开发,用的是ssh框架里面自己整合了hibernate 和Struts2 和spring框架,其中 ...
- Flink Metrics 源码解析
Flink Metrics 有如下模块: Flink Metrics 源码解析 -- Flink-metrics-core Flink Metrics 源码解析 -- Flink-metrics-da ...
- TensorFlow Data模块
模块作用 tf.data api用于创建训练前导入数据和数据处理的pipeline,使得处理大规模数据,不同数据格式和复杂数据处理变的容易. 基本抽象 提供了两种基本抽象:Dataset和Iterat ...
- 前端项目优化 -Web 开发常用优化方案、Vue & React 项目优化
github github-myBlob 从输入URL到页面加载完成的整个过程 首先做 DNS 查询,如果这一步做了智能 DNS 解析的话,会提供访问速度最快的 IP 地址回来 接下来是 TCP 握手 ...
- 记几个 DOM 操作技巧
使用 attributes 属性遍历元素特性 // 迭代元素的每一个特性,将它们构造成 name = value 的字符串形式 function outputAttributes (element) ...
- luoguP3588_[POI2015]PUS
题意 有一个\(n\)个数的序列,已知其中的\(k\)个数,然后有\(m\)个信息,每个信息给出区间\([l,r]\),和\(k\)个数,表示区间\([l,r]\)中这\(k\)个数大于剩下的\(r- ...