HDU 5338 ZZX AND PERMUTATIONS 线段树
多校题解
胡搞。。
。
题意太难懂了。
。
ZZX and Permutations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 310 Accepted Submission(s): 83
ZZX knows that a permutation can be decomposed into disjoint cycles(see https://en.wikipedia.org/wiki/Permutation#Cycle_notation). For example:
145632=(1)(35)(462)=(462)(1)(35)=(35)(1)(462)=(246)(1)(53)=(624)(1)(53)……
Note that there are many ways to rewrite it, but they are all equivalent.
A cycle with only one element is also written in the decomposition, like (1) in the example above.
Now, we remove all the parentheses in the decomposition. So the decomposition of 145632 can be 135462,462135,351462,246153,624153……
Now you are given the decomposition of a permutation after removing all the parentheses (itself is also a permutation). You should recover the original permutation. There are many ways to recover, so you should find the one with largest lexicographic order.
the number of test cases.
Then t testcases
follow. In each testcase:
First line contains an integer n,
the size of the permutation.
Second line contains n space-separated
integers, the decomposition after removing parentheses.
n≤105.
There are 10 testcases satisfying n≤105,
200 testcases satisfying n≤1000.
numbers in a line for each testcase.
Don't output space after the last number of a line.
2
6
1 4 5 6 3 2
2
1 2
4 6 2 5 1 3
2 1
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, ll> pii;
const double eps = 1e-9;
const int N = 200000 + 10;
#define L(x) tree[x].l
#define R(x) tree[x].r
#define M(x) tree[x].ma
#define ls (id<<1)
#define rs (id<<1|1)
struct node {
int l, r;
int ma;
}tree[N << 2];
int a[N], p[N];
void Up(int id) {
M(id) = max(M(ls), M(rs));
}
void build(int l, int r, int id) {
L(id) = l; R(id) = r;
if (l == r) { M(id) = a[l];return; }
int mid = (l + r) >> 1;
build(l, mid, ls); build(mid + 1, r, rs);
Up(id);
}
void update(int pos, int id) {
if (L(id) == R(id))
{
M(id) = -1;return;
}
int mid = (L(id) + R(id)) >> 1;
if (pos <= mid)update(pos, ls);
else update(pos, rs);
Up(id);
}
int query(int l, int r, int id) {
if (l == L(id) && R(id) == r)return M(id);
int mid = (L(id) + R(id)) >> 1;
if (r <= mid)return query(l, r, ls);
else if (mid < l)return query(l, r, rs);
else return max(query(l, mid, ls), query(mid + 1, r, rs));
}
int n;
int use[N], num[N];
pii b[N];
int ans[N];
void getcir(int l, int r) {
if (l > r)return;
for (int i = l; i <= r; i++) {
if (use[a[i]])continue;
int to = i + 1;
if (to > r) to = l;
ans[a[i]] = a[to];
use[a[i]] = 1;
num[a[to]] = 1;
update(i, 1);
}
}
int getmax(int l, int r) {
if (l > r)return -1;
return query(l, r, 1);
}
int hehe[N];
set<int>s;
int main() {
int T;rd(T);
while (T--) {
s.clear();
s.insert(0);
rd(n);
for (int i = 1; i <= n; i++) {
rd(a[i]);
p[a[i]] = i;
use[i] = num[i] = false;
b[i] = { a[i], i };
ans[i] = 0;
}
build(1, n, 1);
sort(b + 1, b + 1 + n);
int top = 0;
for (int i = 1; i <= n; i++) {
if (use[i])continue;
int idx = b[i].second;
int t[3] = { -1, -1, -1 };
if (idx < n && !num[a[idx+1]])t[0] = a[idx + 1];
top = -(*s.upper_bound(-idx));
t[1] = getmax(top + 1, idx - 1);
if (num[i]==false)t[2] = i;
if (t[0] > max(t[1], t[2]))
{
ans[i] = t[0]; use[i] = 1;
num[t[0]] = 1;
update(idx + 1, 1);
}
else if (t[1] > max(t[0], t[2]))
{
getcir(p[t[1]], idx);
s.insert(-idx);
}
else {
getcir(idx, idx);
s.insert(-idx);
}
}
for (int i = 1; i <= n; i++)
{
pt(ans[i]);i == n ? putchar('\n') : putchar(' ');
}
}
return 0;
}
/*
99
3
1 3 2
ans: 3 2 1
5
1 5 2 3 4
ans: 5 3 4 2 1 5
5 2 3 4 1
ans : 5 3 4 1 2 7
6 7 1 3 2 5 4
ans:7 5 3 4 2 6 1 1
8
1 3 6 4 8 7 2 5 1
5
3 2 4 5 1 */
HDU 5338 ZZX AND PERMUTATIONS 线段树的更多相关文章
- HDU 5338(ZZX and Permutations-用线段树贪心)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations
题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
随机推荐
- PCB WCF Web接口增减参数后,在客户端不更新的情况,是否影响客户端,评估测试
1.目的:由于接口众多,服务端变更接口,会造成服务停用更新,造成客户端不能使用或报错, 在此评估[Web中心]此服务端,接口接口参数增加或减少,是否对客户端造成影响 2.评估内容:服务端增加单值参数, ...
- IE兼容性測試軟件
对于前端开发工程师来说,确保代码在各种主流浏览器的各个版本中都能正常工作是件很费时的事情,幸运的是,有很多优秀的工具可以帮助测试浏览器的兼容性,让我们一起看看这些很棒的工具. Spoon Browse ...
- Android Gradle 学习笔记(七):Android Gradle 插件
我们知道Android Gradle其实就是一个Gradle的一个第三方插件,它是由Google的Android团队开发的,基于Gradle构建的,和Android Studio完美搭配.相比于旧的构 ...
- Python迭代器(斐波拉切数列实例)
将一个容器通过iter()函数处理后,就变成了迭代器.迭代器有2个魔法方法__iter__.__next__,一个迭代器必须实现__iter__,这个方法实际上是返回迭代器本身(return self ...
- ROS-URDF-Gazebo
前言:在gazebo里运行urdf文件 一.安装教程包 cd ~/catkin_test/srcgit clone https://github.com/ros/urdf_sim_tutorial.g ...
- 数组中hashCode就是内存地址,以及汉字幻化为16进制或10进制
int[] arr4={1,2,3,4,5}; System.out.println("arr4: "+arr4); System.out.println("arr4.h ...
- js动态追加的元素如何触发事件
一般通过js或者jQuery动态添加的元素标签,通过该元素标签.class.id触发事件,是无效的.如下所示: <body> <input type="text" ...
- Android 关于Fragment重叠问题分析和解决
一.问题描述 相信大家在使用Fragment的过程中,肯定碰到过Fragment重叠的问题,重启应用就好了.然而原因是什么呢? 二.原因分析 首先,Android管理Fragment有两种方式,使用a ...
- java编程基础篇-------> 从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度。如果是 12 月、1 月、2 月,就属于冬季。
从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度.如果是 12月.1 月.2 月,就属于冬季:如果是 3 月.4 月.5 月,就属于春季:如果是 6 月.7 月.8 月,就属于夏季: ...
- 怎样批量删除PDF文件中的注释
日常我们在阅读一些PDF文章时候,我们会发现有些PDF文章带有非常多的注释,显得非常不美观,影响了阅读体验.那么PDF文章里的批注应该怎么进行删除呢?怎样批量删除PDF文件中的注释? 操作教程: ...