[BZOJ5449] 序列
题目链接:序列
Description
给定一个\(1\)~\(n\)的排列x,每次你可以将 \(x_1, x_2, ..., x_i\) 翻转。
你需要求出将序列变为升序的最小操作次数。
多组数据。
数据范围 \(T=5,1\le n\le 25\)
时间限制 \(10\ s\)
Solution
数据范围这么小,我们考虑用\(IDA*\)优化爆搜。
定义估价函数 \(h()=\sum_{i=2}^{n} [abs(x_i-x_{i-1})≠ 1]\)
考虑每一次翻转,最多可以使得\(abs≠1\)的对数减一,所以可以拿\(h()\)来进行估价。
直接爆搜即可。
复杂度 \(O(能过)\)
Code
// Author: wlzhouzhuan
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
inline int read() {
int x = 0, neg = 1; char op = getchar();
while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
return neg * x;
}
inline void print(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
int a[26], flag, n, maxd;
int h() {
int cnt = 0;
for (rint i = 2; i <= n; i++) if (abs(a[i] - a[i - 1]) != 1) {
cnt++;
}
return cnt;
}
bool check() {
for (rint i = 2; i <= n; i++) {
if (a[i - 1] > a[i]) {
return 0;
}
}
return 1;
}
void dfs(int x, int last) { // x表示操作次数,last表示上一次翻转的位置
if (flag) return ;
if (x == maxd) {
if (check()) flag = 1;
return ;
}
if (x + h() > maxd) {
return ;
}
for (rint i = 2; i <= n; i++) if (i != last) {
reverse(a + 1, a + i + 1);
dfs(x + 1, i);
reverse(a + 1, a + i + 1);
}
}
int main() {
int T = read();
while (T--) {
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
}
flag = 0;
for (maxd = 0; maxd <= 30; maxd++) {
dfs(0, 0);
if (flag) break;
}
printf("%d\n", maxd);
}
return 0;
}
[BZOJ5449] 序列的更多相关文章
- 【夯实PHP基础】UML序列图总结
原文地址 序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色 ...
- Windows10-UWP中设备序列显示不同XAML的三种方式[3]
阅读目录: 概述 DeviceFamily-Type文件夹 DeviceFamily-Type扩展 InitializeComponent重载 结论 概述 Windows10-UWP(Universa ...
- 软件工程里的UML序列图的概念和总结
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 软件工程的一般开发过程:愿景分析.业务建模,需求分析,健壮性设计,关键设计,最终设计,实现…… 时序图也叫序列图(交互图),属于软件 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- 最长不下降序列nlogn算法
显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- 前端react+redux+koa写的博客推荐
React-Node搭建的博客 曾经用的php+mysql+js写的博客,现在看来已经很low了,所以用目前最火的react+koa框架重构一下.先上地址吧:目前线上版本http://www.liuw ...
- px,rem,em 通过媒体查询统一的代码
@media only screen and (max-width: 1080px), only screen and (max-device-width:1080px) { html,body { ...
- 常见的JVM 面试题
1.讲一讲JVM的跨平台与跨语言 跨平台 我们写的一个类,在不同的操作系统上(Linux.windows.Mac OS)执行,效果是一样的.这就是JVM的跨平台性. 跨语言 JVM只识别字节码,JVM ...
- Python中用函数实现代码的复用
# Python中用函数实现代码复用 """ def funcname(paras): statements return [expression] 关于函数定义说明如下 ...
- Spring Boot-@Value获取值和@ConfigurationProperties获取值的比较
@Value和@ConfigurationProperties都是用于属性的注入(相当于spring中<bean id=" " class=" "> ...
- power app 解决方案中表导入问题
我们在powerapp中导出的表,解压后是会是乱码,导致在导入的时候会失败,或者导入数据不全. 使用 2 但是直接导入也会是乱码,所以需要将文件重新保存一下: 首先新建一个excel 选择要导入的那个 ...
- zookeeper面试1-9
1.选举机制 SID:服务器ID.用来唯一标识一台ZooKeeper集群中的机器,每台机器不能重复,和myid一致. ZXID:事务ID.ZXID是一个事务ID,用来标识一次服务器状态的变更.在某一时 ...
- 关于Swagger优化
背景 尽管.net6已经发布很久了,但是公司的项目由于种种原因依旧基于.net Framework.伴随着版本迭代,后端的api接口不断增多,每次在联调的时候,前端开发叫苦不迭:"小胖,你们 ...
- 音视频基本概念和FFmpeg的简单入门
写在前面 最近正好有音视频编辑的需求,虽然之前粗略的了解过FFmpeg不过肯定是不够用的,借此重新学习下: 基本概念 容器/文件(Conainer/File): 即特定格式的多媒体文件,一般来说一个视 ...
- XCTF练习题---WEB---view_source
XCTF练习题---WEB---view_source flag:cyberpeace{662b1cf989a0a7999a5589290ce5a88e} 解题步骤: 1.观察题目,打开场景 2.根据 ...