地址 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. Provide Several View Variants for End-Users 为最终用户提供多个视图变体

    In this lesson, you will learn how to provide several customized variants of the same View, and allo ...

  2. Axure导出的原型无法在谷歌浏览器浏览

    1.下载crx后缀的文件. 2.修改crx后缀名为rar的压缩文件 3.解压刚才的rar文件 4.打开谷歌浏览器右上角的三个点 更多工具==>扩展程序 选择刚才的解压文件夹. 上面的图表示安装成 ...

  3. library: Vulnhub Walkthrough

    网络主机探测: 端口主机扫描: ╰─ nmap -p1-65535 -sV -A -O -sT 10.10.202.136 21/tcp open ftp vsftpd 3.0.380/tcp ope ...

  4. 与用户xxx一起提供的密码不正确。请确认输入的密码正确并重试

    环境: SharePoint 2010 / 2013 以系统账户身份登录管理中心,然后创建Web Application,报: 与用户xxx一起提供的密码不正确.请确认输入的密码正确并重试 明明都已经 ...

  5. canvas在vue中的应用

    使用cavas可以绘制各种图表.生成二维码.制作H5小游戏. 生命周期 canvas应该在mounted的生命周期中初始化,在updated中是无效的. export default { mounte ...

  6. 导入Jar报错An internal error occurred during: "Building workspace". zip END header not found

    百度了好久都没有找到答案,后来新建了一个文件夹,再build path就正常了,不知道为什么

  7. 使用webstrom开发小程序要做的设置

    1.关闭rpx的错误提示 在setting里面  -->搜索inspections --> 在右侧找到invalid CSS property value    把对勾划掉

  8. nlp英文的数据清洗代码

    1.常用的清洗方式 #coding=utf-8 import jieba import unicodedata import sys,re,collections,nltk from nltk.ste ...

  9. Sass、LESS 和 Stylus各有千秋

    废话不多说直接上连接  为您详细比较三个 CSS 预处理器(框架):Sass.LESS 和 Stylus  

  10. 埃氏筛法(求n以内有哪些个质数)

    核心思想:从i=2开始,划去i的倍数,即剩下i为质数(如删去2的倍数后2为质数,再删去3的倍数后3为质数,4被删除则跳过,5未被删除则记录然后删除5的倍数...以此类推) #include <b ...