【hdu 6351】Beautiful Now
【链接】 我是链接,点我呀:)
【题意】
你可以最多交换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的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 3537】Daizhenyang's Coin
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- [Javascript Crocks] Create a Maybe with a `safe` Utility Function
In this lesson, we’ll create a safe function that gives us a flexible way to create Maybes based on ...
- [SCSS] Convert SCSS Variable Arguments to JavaScript
We will learn how to convert variable arguments by using rest operator in JavaScript. .sass-btn { co ...
- 2015年北京大学软件project学科优秀大学生夏令营上机考试---C:单词翻转面试题
题目描写叙述:翻转句子中单词的顺序.但单词内字符的顺序不变.句子中单词以空格符隔开. 为简单起见,标点符号和普通字母一样处理.如:"I am a student."翻转成" ...
- C++实现顺序栈的基本功能
栈是限定仅在表头进行插入和删除操作的线性表.有着先进后出的特点(FILO): 如今我来动手实现栈的基本本功能练练手: 定义栈的头文件例如以下: #ifndef CSTOCK_H_ #define CS ...
- Thinking in Java:容器深入研究
1.虚线框表示Abstract类,图中大量的类的名字都是以Abstract开头的,它们仅仅是部分实现了特定接口的工具,因此创建时能够选择从Abstract继承. Collections中的实用方法:挑 ...
- 搜索分析(DFS、BFS、递归、记忆化搜索)
搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...
- html5 初探
html5是越来越火了.小小菜鸟也来学习学习. 相比于之前的几个版本,HTML5提供了更加丰富的多媒体标签使得音乐,视频的播放不用再借助于flah了.不过暂时各浏览器间样式还是有差别. 除此之外,表单 ...
- [JavaEE] Spring事务配置的五种方式
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...
- css3 背景background
Css3背景<background> Css3包含多个新的背景属性,它们提供了对背景更强大的控制.可以自定义背景图的大小,可以规定背景图片的定位区域,css3还允许我们为元素使用多个背景图 ...
- 09-JavaScript高级
今日知识 1. Dom(文档对象模型) 2. Bom(浏览器对象模型) 3. 总结 Dom 1. 获取id为div1的元素对象. * var result=document.getElementByI ...