[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 ...
随机推荐
- JS 用状态机的思想看Generator之基本语法篇
前言 最近学习了阮一峰老师的<ECMAScript 6 入门>里的Generator相关知识,以及<你不知道的JS>中卷的异步编程部分.同时在SegmentFault问答区看到 ...
- 前端面试题整理——Javascript基础
常见值类型: let a; //undefined let s = 'abc'; let n = 100; let b = true; let sb = Symbol('s'); let nn = N ...
- 【weex开发】weex官方源码
公司目前使用版本:weex_sdk:0.10.0 介绍地址:https://bintray.com/alibabaweex/maven/weex_sdk/0.18.0 weex最新版本:weex_sd ...
- 将word文件转为excel文件
有些word文件里的数据是有顺序或者规律,想转成表格的形式,下面就以我要转的word为例. 我的word文件是这样的 1.word转txt(文本文件) 文件--->另存为--->路径--- ...
- PL/SQL中的 not
ELECT * FROM table_name WHERE column_name not like'%山%' 這時出現了column_name中為null值的情況也被剔掉了. 原因是:在SQL的表達 ...
- EMS修改邮箱容量限制的方法
使用PowerShell命令完成邮箱数据库限制任务. 以Exchange管理员身份打开EMS控制台.在PowerShell命令提示符下,键入如下命令. Set-MailboxDatabase Test ...
- Spring Security 一键接入验证码登录和小程序登录
最近实现了一个多端登录的Spring Security组件,用起来非常丝滑,开箱即用,可插拔,而且灵活性非常强.我觉得能满足大部分场景的需要.目前完成了手机号验证码和微信小程序两种自定义登录,加上默认 ...
- HTML5有哪些更新(部分)
1. 语义化标签 header:定义文档的页眉(头部): nav:定义导航链接的部分: footer:定义文档或节的页脚(底部): article:定义文章内容: section:定义文档中的节(se ...
- k8s pod故障分类与排查
一.Pod故障状态基本有几种Pod状态 处于PendingPod状态 处于WaitingPod状态 处于ContainerCreatingPod状态 ImagePullBackOffPod状态 Cra ...
- JavaWeb学习day5-Servlet初学