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

【题意】

你可以最多交换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;
}

最新文章

  1. linux下查看和添加PATH环境变量
  2. Yii 动作过滤的方法
  3. 《Linux内核分析》第八周 进程的切换和系统的一般执行过程
  4. laravel框架总结(十四) -- 数据迁移和数据填充
  5. 深入理解js——继承
  6. 《单页Web应用--温故JavaScrpt》学习笔记整理
  7. javascript模块化编程(AMD规范的加载器)
  8. (转)《深入理解java虚拟机》学习笔记6——类加载机制
  9. SAP ABAP 处理字符串串串串串串串串(详细)
  10. SecureCRT恢复默认字体
  11. 常用的JQuery数字类型验证正则表达式
  12. Python - 缩写(capwords) 和 创建转换表(maketrans) 详细说明
  13. android 监听返回键
  14. (转)学习MySQL优化原理,这一篇就够了!
  15. Python:logging.NullHandler 的使用
  16. ZooKeeper-3.3.4集群安装配置
  17. Linux 查看端口被什么程序占用
  18. cJSON精度丢失问题
  19. php 访问错误日志
  20. 在Emacs中启用Fcitx输入法

热门文章

  1. [Unit Testing] Set the timeout of a Test in Mocha
  2. POJ 3280 Cheapest Palindrome DP题解
  3. Service启动模式
  4. Qt的Socket数据通讯的一个样例。
  5. 【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景
  6. SWERC13 Trending Topic
  7. Highcharts构建空饼图
  8. IOS UITextView光标位置在中间的问题
  9. Objective-c基础知识学习笔记
  10. bootstrap搜索样式