【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 ...
随机推荐
- 让cocos2dx支持并通过arm64 编译
为了要支持64位,请把这个文件直接替换到相应的lib文件夹下.本来是须要改neton_matrix_impl.c里的宏定义, 在 platform/ios/EAGLVIEW.mm中 在neon_mat ...
- 18110 Koishi's travel, Satori's travel
18110 Koishi's travel, Satori's travel 该题有题解 时间限制:4000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 ...
- linux git保存用户名密码(避免每次push输用户名密码)
Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示: machine your-git-server log ...
- html5之文件操作
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
- php navigat备份
点击查询->新建查询->写sql查询出更新的数据部分(根据时间等条件) -> 点击上方工具菜单栏的导出向导 ,然后就可以根据选择导出文件了可以导出sql脚本excel等很多,绝对有你 ...
- 负载均衡获得真实源IP的6种方法
除了X-FORWARD-FOR,负载均衡中获得真实源IP的方法还有很多种. 本文抛砖引玉,主要介绍获得真实源IP的多种方法,而不是具体配置. 负载均衡获得真实IP的方法有很多种,将形成专题文章. 本文 ...
- mysql视图,索引和存储过程
一:视图 视图又叫虚表.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成. 具体 ...
- 44.Qt通过子类化qstyle实现自定义外观
main.cpp #include <QtGui> #include "brozedialog.h" #include "bronzestyle.h" ...
- The name ‘InitialzeComponent’ does not exist in the current context
在Visual Studio中创建Windows Store项目,在MainPage.xaml.cs中出现错误: The name 'InitialzeComponent' does not exis ...
- deepin下jdk和tomcat的安装教程
在deepin上安装java是真的心累啊,照着网上的教程弄,结果一团糟.好不容易折腾成功了,记录下来. 1.下载jdk 首先我们要知道,用sudo 类似命令下载的jdk,是open的jdk,是开源的, ...