【链接】 我是链接,点我呀:)

【题意】

你可以最多交换k次数字。
让你组成一个最大的和一个最小的数字。

【题解】

直接写个bfs.求出所有状态的最小交换次数。
但是最大值和最小值分开写。
做最大值的时候。
假设要交换x[i],x[j] (ix[j]才交换。
加上这个优化就能过了。
直接输出最小值和最大值就ok了。

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define res(x) scanf("%s",x)
#define rson mid+1,r,rt<<1|1
using namespace std; const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int M = 40e4; int k,len,xx[15];
string s,ss;
map<int,int> mmap;
int h,t,dl[M+10]; int _change(string x){
int len = x.size();
int xx = 0;
for (int i = 0;i < len;i++){
xx = xx*10 + (x[i]-'0');
}
return xx;
} int _change(int xx[15]){
int x = 0;
for (int i = 1;i <= len;i++){
x = x*10 + xx[i];
}
return x;
} void _swap(int &x,int &y){
int t = x;x = y;y = t;
} int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
mmap.clear();
cin >> s >> k;
len = s.size();
int dd = _change(s);
mmap[dd] = 0;
h = 1,t = 1;dl[1] = dd;
while (h<=t){
int x = dl[h++];
int now = mmap[x];
for (int i = len;i >= 1;i--) {
xx[i] = x%10;
x/=10;
}
if (now+1>k) break;
for (int i = 1;i <= len;i++)
for (int j = i+1;j <= len;j++)
if (xx[i]>xx[j]){
_swap(xx[i],xx[j]);
if (xx[1]!=0) {
int tx = _change(xx);
if(mmap.find(tx)==mmap.end()){
mmap[tx] = now+1;
dl[++t] = tx;
}
}
_swap(xx[i],xx[j]);
}
} cout<<(*mmap.begin()).first<<' '; mmap.clear();
dd = _change(s);
mmap[dd] = 0;
h = 1,t = 1;dl[1] = dd;
while (h<=t){
int x = dl[h++];
int now = mmap[x];
for (int i = len;i >= 1;i--) {
xx[i] = x%10;
x/=10;
} if (now+1>k) break;
for (int i = 1;i <= len;i++)
for (int j = i+1;j <= len;j++)
if (xx[i]<xx[j]){
_swap(xx[i],xx[j]);
if (xx[1]!=0) {
int tx = _change(xx);
if(mmap.find(tx)==mmap.end()){
mmap[tx] = now+1;
dl[++t] = tx;
}
}
_swap(xx[i],xx[j]);
}
} map<int, int>::iterator it = mmap.end();
it--;
cout<<(*it).first<<endl;
}
return 0;
}

【hdu 6351】Beautiful Now的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 3537】Daizhenyang's Coin

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. HDU 2604 Queuing,矩阵高速幂

    题目地址:HDU 2604 Queuing 题意:  略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...

  2. ios业务模块间互相跳转的解耦方案

    *此文章需有一点runtime的知识,假设你不了解runtime,<高速理解Runtime of Objective-C>: http://mp.weixin.qq.com/s?__biz ...

  3. SRM 622 D2L3: Subsets, math, backtrack

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855 符合条件的集中非1的元素个数是非 ...

  4. Linux命令(四)——文件权限管理

    文件权限是指对文件的访问控制,即哪些用户或群组可以访问文件以及执行什么样的操作. 一.文件的权限 1.Linux文件类型 (1)普通文件:文本文件+数据文件+可执行的二进制文件. (2)目录文件:即文 ...

  5. ueditor1.4.3在.net环境下的vs开发工具中集成经验

    Ueditor是个非常不错的在线富文本编辑器,几个项目一直使用它.近期想更新版本号.发现新版1.4.3与旧版的部署方式全然不一样了.官网文档介绍的是直接放在iis下的部署说明,没有提到在vs开发工具中 ...

  6. luogu1771 方程的解

    题目大意 对于不定方程a1+a2+…+ak-1+ak=g(x),其中k≥2且k∈N,x是正整数,g(x)=x^x mod 1000(即x^x除以1000的余数),x,k是给定的数.我们要求的是这个不定 ...

  7. android.mk中LOCAL_MODULE_TAGS说明【转】

    转自http://blog.csdn.net/evilcode/article/details/6459299 LOCAL_MODULE_TAGS :=user eng tests optional ...

  8. netpbm开机logo制作工作【转】

    本文转载自: http://www.fx114.net/qa-19-74437.aspx

  9. devenv.exe 编译Solution

    Build https://docs.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe Builds a solution ...

  10. TensorRT加速 ——NVIDIA终端AI芯片加速用,可以直接利用caffe或TensorFlow生成的模型来predict(inference)

    官网:https://developer.nvidia.com/tensorrt 作用:NVIDIA TensorRT™ is a high-performance deep learning inf ...