【hdu 6351】Beautiful Now
HDU NOW Beautiful
2023-09-14 09:03:44 时间
【链接】 我是链接,点我呀:)
【题意】
【题解】
直接写个bfs.求出所有状态的最小交换次数。 但是最大值和最小值分开写。 做最大值的时候。 假设要交换x[i],x[j] (i【代码】
#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 1556-差分数组和线段树的对比分析-Color the ball
- hdu 3980 Paint Chain(SG函数)
- HDU 3336 KMP算法中对next数组的理解「建议收藏」
- HDU P3341 Lost’s revenge 题解+数据生成器
- hdu 3336 Count the string(kmp应用)
- hdu 3336 Count the string 用心写的题解
- hdu 1142_hdu1001
- HDU 3336 Count the string 解题报告
- [ACM] HDU 1006 解题报告
- H - Partial Tree HDU - 5534 【 完全背包 】
- HDU-2084 数塔
- 敌兵布阵(HDU 1166)
- I Hate It (HDU 1754)
- 校庆神秘建筑(HDU 1411)
- 还是畅通工程(HDU 1233)
- Friend-Graph (HDU 6152)2017中国大学生程序设计竞赛 - 网络选拔赛
- CaoHaha's staff (HDU 6154)(2017中国大学生程序设计竞赛 - 网络选拔赛)
- Oil Deposits (HDU - 1241 )(DFS思路 或者 BFS思路)
- 畅通工程续(HDU 1874)(简单最短路)
- Drainage Ditches (HDU - 1532)(最大流)
- Ancient Go ( HDU - 5546 ) ( BFS 搜索是否相连)
- 统计难题 【 HDU - 1251 】【 字典树 】
- Phone List 【 HDU - 1671 】 【 字典树判断是否存在前缀 】
- A * B Problem Plus【HDU 1402】 【FFT求高精度乘法】
- Bone Collector HDU - 2602【 01 背包 】
- MySQL中的NOW函数:驱动时间序列操作(mysql的now函数)
- 立刻用MySQL NOW函数满足你的时间需求(mysql now 函数)
- MYSQL中的NOW函数使用方法(mysql中now用法)
- MySQL中的Now方法详解(mysql中now方法)
- Oracle现在拥有更强大的NOW函数(oracle now函数)