地址 https://algospot.com/judge/problem/read/SORTGAME

解答

常规BFS是会超时的  按照书上的提示 应该是打表(居然还有提倡打表的题目)

tle 代码

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map> using namespace std; int n, m; int bfs(const vector<int>& v)
{
vector<int> sorted = v;
sort(sorted.begin(), sorted.end());
queue<vector<int>> q;
map<vector<int>, int> distance;
distance[v] = ;
q.push(v);
while (!q.empty()) {
vector<int> here = q.front();
q.pop(); if (here == sorted) return distance[here];
int cost = distance[here];
//翻转可能的子区间
for (int i = ; i < v.size(); i++) {
for (int j = i + ; j <= v.size(); ++j) {
reverse(here.begin() + i, here.begin() + j);
if (distance.count(here) == ) {
distance[here] = cost + ;
q.push(here);
}
reverse(here.begin() + i, here.begin() + j);
}
}
} return -;
} int main()
{
cin >> n; while (n--) {
cin >> m;
vector<int> v;
for (int i = ; i < m; i++) {
int t;
cin >> t;
v.push_back(t);
} cout << bfs(v) << endl;
} return ;
}

ac代码

 #include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector> using namespace std; map<vector<int>, int> toSort; void precalc(int n) {
vector<int> perm(n);
for (int i = ; i < n; ++i) perm[i] = i;
queue<vector<int>> q;
q.push(perm);
toSort[perm] = ;
while (!q.empty()) {
vector<int> here = q.front();
q.pop();
int cost = toSort[here];
for (int i = ; i < n; i++) {
for (int j = i + ; j <= n; ++j) {
reverse(here.begin() + i, here.begin() + j);
if (toSort.count(here) == ) {
toSort[here] = cost + ;
q.push(here);
}
reverse(here.begin() + i, here.begin() + j);
}
}
}
} int solve(const vector<int>& perm)
{
int n = perm.size();
vector<int> fixed(n);
for (int i = ; i < n; ++i) {
int smaller = ;
for (int j = ; j < n; ++j)
if (perm[j] < perm[i])
++smaller;
fixed[i] = smaller;
}
return toSort[fixed];
} int n, m;
int main()
{
cin >> n;
while (n--) {
cin >> m;
precalc(m);
vector<int> perm;
for (int i = ; i < m; i++) {
int t;
cin >> t;
perm.push_back(t);
}
cout << solve(perm) << endl;
} return ;
}

算法问题实战策略 SORTGAME的更多相关文章

  1. 算法问题实战策略 PICNIC

    下面是另一道搜索题目的解答过程题目是<算法问题实战策略>中的一题oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/PICNIC ...

  2. 《算法问题实战策略》-chaper7-穷举法

    关于这一章节<算法实战策略>有一段概述问题,我认为对于编程人员来说非常有价值,故在这里进行如下的摘抄: 构想算法是很艰难的工作.相比大家都经历过,面对复杂的要求只是傻乎乎地盯着显示器,或者 ...

  3. 《算法问题实战策略》-chaper32-网络流

    基本的网络流模型: 在图论这一块初步的应用领域中,两个最常见的关注点,其一时图中的路径长度,也就是我们常说的的最短路径问题,另一个则是所谓的“流问题”. 流问题的基本概念: 首先给出一张图. 其实所谓 ...

  4. 《算法问题实战策略》-chaper13-数值分析

    这一章节主要介绍我们在进行数值分析常用的二分.三分和一个近似求解区间积分的辛普森法. 首先介绍二分. 其实二分的思想很好理解并且笔者在之前的一些文章中也有所渗透,对于二次函数甚至单元高次函数的零点求解 ...

  5. 《算法问题实战策略》——chaper9——动态规划法技巧

    Q1: 数字游戏: 两个人(A.B)用n个整数排成的一排棋盘玩游戏,游戏从A开始,每个人有如下操作: (1)    拿走棋盘最右侧或者最左侧的棋子,被拿走的数字从棋盘中抹掉. (2)    棋盘中还剩 ...

  6. 《算法问题实战策略》-chaper8-动态规划法

    Q1:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...

  7. 《算法问题实战策略》-chaper21-树的实现和遍历

    这一章节开始介绍一个数据结构中的一个基本概念——树. 我们从数据结构的解读来解释树结构的重要性,现实世界的数据除了最基本的线性结构(我们常用队列.数组和链表等结构表征),还有一个重要的特性——层级结构 ...

  8. 算法问题实战策略 QUADTREE

    地址 https://algospot.com/judge/problem/read/QUADTREE 将压缩字符串还原后翻转再次压缩的朴素做法 在数据量庞大的情况下是不可取的 所以需要在压缩的情况下 ...

  9. 算法问题实战策略 DICTIONARY

    地址 https://algospot.com/judge/problem/read/DICTIONARY 解法 构造一个26字母的有向图 判断无回路后 就可以输出判断出来的字符序了 比较各个字母的先 ...

随机推荐

  1. Flask 教程 第十二章:日期和时间

    本文翻译自The Flask Mega-Tutorial Part XII: Dates and Times 这是Flask Mega-Tutorial系列的第十二部分,我将告诉你如何以适配所有用户的 ...

  2. go笔记--json包使用

    目录 Marshal Unmarshal 处理json对象 @ json包实现了json对象的编解码,参见RFC 4627.Json对象和go类型的映射关系主要通过Marshal和Unmarshal函 ...

  3. C lang:Protect array data——Const

    Xx_Introduction Use pointer translate parameter array original data will change data,and use const p ...

  4. tensorflow 性能调优相关

    如何进行优化tensorflow 将极大得加速机器学习模型的训练的时间,下面是一下tensorflow性能调优相关的阅读链接: tensorflow 性能调优:http://d0evi1.com/te ...

  5. java之集合工具类Collections

    Collections类简介 java.utils.Collections 是集合工具类,用来对集合进行操作.此类完全由在 collection 上进行操作或返回 collection 的静态方法组成 ...

  6. IntelliJ IDEA安装与使用

    官网:https://www.jetbrains.com/ 点击 点击下载 点击

  7. Java之ssh框架spring配置文件配置定时任务

    最近做了一个数据同步功能,要求晚上0点去定时同步数据,这是个老项目框架用的ssh,定时任务基于quartz,废话不多说,下面详细说说相关配置. 在spring的配置文件中: <!-- 0点定时任 ...

  8. C语言中的顺序点

    C语言盲点1.函数参数的求值顺序依赖于编译器,例如f(a,a++);是先求a++还是求a不一定 2.C语言中的大多数运算符对其操作数的求值顺序也依赖于编译器 警告int i = f() * g();这 ...

  9. go语言之goto语句和函数和defer语句

    1.goto关键字 import "fmt" func main() { for i := 0;i <11;i++{ if i == 2{ //关键字,goto跳转到某个位置 ...

  10. 动态数组原理【Java实现】(六)

    前言 接下来我们进入集合学习,看过很多文章一上来就是讲解原理感觉会特别枯燥,任何成熟解决方案的出现都是为了解决问题,若通过实际问题引入然后再来讲解原理想必学起来必定事半功倍,从我写博客的那一天起,我就 ...