算法问题实战策略 SORTGAME
地址 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的更多相关文章
- 算法问题实战策略 PICNIC
下面是另一道搜索题目的解答过程题目是<算法问题实战策略>中的一题oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/PICNIC ...
- 《算法问题实战策略》-chaper7-穷举法
关于这一章节<算法实战策略>有一段概述问题,我认为对于编程人员来说非常有价值,故在这里进行如下的摘抄: 构想算法是很艰难的工作.相比大家都经历过,面对复杂的要求只是傻乎乎地盯着显示器,或者 ...
- 《算法问题实战策略》-chaper32-网络流
基本的网络流模型: 在图论这一块初步的应用领域中,两个最常见的关注点,其一时图中的路径长度,也就是我们常说的的最短路径问题,另一个则是所谓的“流问题”. 流问题的基本概念: 首先给出一张图. 其实所谓 ...
- 《算法问题实战策略》-chaper13-数值分析
这一章节主要介绍我们在进行数值分析常用的二分.三分和一个近似求解区间积分的辛普森法. 首先介绍二分. 其实二分的思想很好理解并且笔者在之前的一些文章中也有所渗透,对于二次函数甚至单元高次函数的零点求解 ...
- 《算法问题实战策略》——chaper9——动态规划法技巧
Q1: 数字游戏: 两个人(A.B)用n个整数排成的一排棋盘玩游戏,游戏从A开始,每个人有如下操作: (1) 拿走棋盘最右侧或者最左侧的棋子,被拿走的数字从棋盘中抹掉. (2) 棋盘中还剩 ...
- 《算法问题实战策略》-chaper8-动态规划法
Q1:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...
- 《算法问题实战策略》-chaper21-树的实现和遍历
这一章节开始介绍一个数据结构中的一个基本概念——树. 我们从数据结构的解读来解释树结构的重要性,现实世界的数据除了最基本的线性结构(我们常用队列.数组和链表等结构表征),还有一个重要的特性——层级结构 ...
- 算法问题实战策略 QUADTREE
地址 https://algospot.com/judge/problem/read/QUADTREE 将压缩字符串还原后翻转再次压缩的朴素做法 在数据量庞大的情况下是不可取的 所以需要在压缩的情况下 ...
- 算法问题实战策略 DICTIONARY
地址 https://algospot.com/judge/problem/read/DICTIONARY 解法 构造一个26字母的有向图 判断无回路后 就可以输出判断出来的字符序了 比较各个字母的先 ...
随机推荐
- JavaScript 标准内置对象Promise使用学习总结
Javascript标准内置对象Promise使用学习总结 by:授客 QQ:1033553122 1. 基础用法 var condition = true; let p = new Prom ...
- diango中有三种response
from django.shortcuts import render, redirect, HttpResponse HttpResponse() render() redirect()
- pytorch 中改变tensor维度的几种操作
具体示例如下,注意观察维度的变化 #coding=utf-8 import torch """改变tensor的形状的四种不同变化形式""" ...
- C++ std::list 基本用法
#include <iostream> #include <string> #include <list> using namespace std; // http ...
- 【洛谷5643】[PKUWC2018] 随机游走(Min-Max容斥+待定系数法+高维前缀和)
点此看题面 大致题意: 从一个给定点出发,在一棵树上随机游走,对于相邻的每个点均有\(\frac 1{deg}\)的概率前往.多组询问,每次给出一个点集,求期望经过多少步能够访问过点集内所有点至少一次 ...
- 用dotnet core 搭建web服务器(一)http server
环境说明 dotnet core,开发需要安装dotnetcore sdk,运行需要安装 dotnetcore runtime 运行目前几乎支持所有常见平台 开发推荐windows10 平台 首先安装 ...
- LinAlgError: Last 2 dimensions of the array must be square
python 矩阵计算时出现错误 此时如果矩阵不是方阵,就会出现如下错误: 这里值得注意的是:当我们这样使用的时候,程序运行又会很正常: 貌似我们求得了非方阵的逆. 下面我们来验证一下: 仔细一看,这 ...
- Spring 框架基础(02):Bean的生命周期,作用域,装配总结
本文源码:GitHub·点这里 || GitEE·点这里 一.装配方式 Bean的概念:Spring框架管理的应用程序中,由Spring容器负责创建,装配,设置属性,进而管理整个生命周期的对象,称为B ...
- SpringCloud的入门学习之Eureka(高可用注册中心HA)构建Provider服务、Consumer服务
1.在高可用的Eureka注册中心中构建provider服务. 使用springboot的多环境配置,来搭建Eureka的高可用集群式部署.由于使用的是maven构建的springboot项目,所以首 ...
- MySql-8.0.16-winx64 安装
参考文章: https://www.cnblogs.com/lxlin/p/9635350.html https://www.cnblogs.com/xc1234/p/9050149.html MyS ...