地址 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. 用Python6种方法:给定一个不超过5位的正整数,判断有几位

    方法一:作比较 a=int(input(">>>>")) if a<10: print(1) elif a<100: #第一个条件已经过滤了大于 ...

  2. How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象

    When designing business classes, a common task is to ensure that a newly created business object is ...

  3. How to: Specify a Display Member (for a Lookup Editor, Detail Form Caption, etc.)如何:指定显示成员(用于查找编辑器、详细信息表单标题等)

    Each business object used in an XAF application should have a default property. The default property ...

  4. Git实战指南----跟着haibiscuit学Git(第三篇)

    笔名:  haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...

  5. .netcore2.1 ef 使用外键关联查询

    //实体类 [Table("invoiceinfo", Schema = "obs")] public class invoice { [Key] public ...

  6. 教程视频、项目源码、全部干货【微信小程序、React Native、Java、iOS、数据结构】

    把我收藏多年的教学视频.项目源码分享给大家,大神就可以忽略了,很多东西都是基础性的,都是期初学习阶段收集的东西. 微信小程序(入门级,有web前端基础的人群): 链接: https://pan.bai ...

  7. 百度大脑UNIT3.0详解之语音语义一体化方案

    在电话客服场景里,用户和机器人交流的过程中,经常会出现沉默.打断机器人.噪声等情况,机器人在应对这些异常情况的时候,需要语音和语义理解技术进行处理,才能实现用户和机器人的流畅交谈.而这些能力的获取与应 ...

  8. 产品经理如何使用 CODING 进行项目规划

    CODING 为您的企业提供从概念到软件开发再到产品发布的全流程全周期软件研发管理,为您的研发团队提供全程助力,帮助研发团队捋清需求.不断迭代.快速反馈并能实时追踪项目进度直到完成.同时 CODING ...

  9. [Go] 使用net包作为tcp客户端读取http

    1.tcp的客户端,并且直接读取http协议的全部内容,每次读取4096字节,直到最后一个字节是\n并且读取的长度小于4096 conn, err := net.Dial("tcp" ...

  10. VM虚拟机安装无法将值写入注册表.....请确认你是否有足够的权限访问该注册表项,或者与技术支持人员联系。

    解决方法: 关掉360安全卫士等软件再安装