[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 ...
随机推荐
- JavaScript实现带正则表达式的表单校验(校验成功后跳转)
运行结果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...
- php文件下载服务器代码
事情的起因 额,平板想下载电脑上的pdf文件,我开启了web服务,局域网下的ipad访问该文件web路径会直接打开该pdf,而不是下载.于是本小白就折腾了一下. 源代码 <?php forceD ...
- SpringMVC-自定义转换器
1.定义转换器类实现Converter接口 import org.springframework.core.convert.converter.Converter; import java.text. ...
- Linux内核--链表结构(二)
Linux内核链表定义了一系列用于链表遍历的宏,本章详细描述. 一.container_of和offsetof 首先介绍两个很好用的宏container_of和offsetof.offsetof宏用于 ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- Typora+PicGO+Gitee实现图床功能
Typora+PicGO+Gitee实现图床功能 版本 typora(0.9.86) PicGo(2.3.0) 主要参考链接 出现问题就先看看这个 问题一 打开PicGo后安装github插件会一直安 ...
- APL 和 Web APL 的概述
APL APl ( Application ProgrammingInterface,应用程序编程接口) 是一些预先定义的函数,目的是提供应用程序 与开发人员基于某软件或硬件得以访问一组例程的能力,而 ...
- 面试突击41:notify是随机唤醒吗?
做 Java 开发的小伙伴,对 wait 方法和 notify 方法应该都比较熟悉,这两个方法在线程通讯中使用的频率非常高,但对于 notify 方法的唤醒顺序,有很多小伙伴的理解都是错误的,有很多人 ...
- 基于Arcgis Engine 10.2(C#)+PostgreSQL 11(Postgis 3)+pgRouting 3.0实现使用数据库进行路径规划
前言:最近在(被迫)使用ArcGIS Engine10.2(.NET平台)进行二次开发(桌面应用),因为想做一个最短路径查询的功能,而arcgis的网络分析又比较麻烦,于是想到了使用Postgis.但 ...
- WinForm中TextBox文本过长解决
方案1: 如果界面有足够的空间 可以使用Multiline属性设置多行 方案2: 可以使用文本框的MouseHover事件,触发弹窗,缺点需要按确定 private void txt_Fnote_M ...