hdu5338 ZZX and Permutations

非原创,来自多校题解

不是自己写的,惭愧ing……

留着以后自己参考……

lower_bound {1,2,4,5} 询问 2,返回的是 2 ,询问3 返回的是 4 是大于等于元素的值

upper_bound {1,2,4,5} 询问2,返回4,询问3,返回4,是 大于 元素的值

题意:图论的知识

1 2 3 4 5 6 (1)

1 4 5 6 3 2 (2)

(1)中 数字 1 的 位置没变 所以(1) 2 的为位置 编程了 4 ,4 的位置变成了 6,6的位置变为 2 即 2 -> 4 -> 6 -> 2 所以(2,4,6)在一个群中而 相应的 4 -> 6 -> 2 -> 4 是等价的 所以也可以表达为 (4,6,2) 最后是 (3,5)

所以 操作为 (1) (2,4,6)(3,5)

问题是:给 你 操作 ,把 括号 去掉,让你安排括号的位置 使 重排 后 的 字典序最大

官方题解:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-4-solutions-by-%E5%AD%A6%E5%86%9B%E4%B8%AD%E5%AD%A6/  第四场 最后一道题

不用线段树会超时……

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
const int MAXN = +;
const int inf = 0x7fffffff;
int a[MAXN],ord[MAXN];
/*cut 该位置后是否加括号
*con 该元素 是否 已经 被 确定在某个括号里面
*/
bool cut[MAXN],con[MAXN];
set<int>cuts;//存放的是左括号和右括号
int n;
int seg[MAXN*];
void cutit(int x){ //加括号
if(!cut[x]){
cut[x] = ;
cuts.insert(x);
}
}
void build(int l,int r,int x){ //建树
if(l == r){
seg[x] = a[r];
return;
}
int mid = (l+r)>>;
build(l,mid,x<<);
build(mid+,r,x<<|);
seg[x] = max(seg[x<<],seg[x<<|]);
}
int v,l1,r1,I;
void que(int l,int r,int x){ //查询区间最值
if(l >= l1 && r <= r1){
v = max(v,seg[x]);
return;
}
int mid = (l+r)>>;
if(mid >= l1){
que(l,mid,x<<);
}
if(mid < r1){
que(mid+,r,x<<|);
}
}
void upd(int l,int r,int x){ //把用掉的值消除掉
if(l == r){
seg[x] = -inf;
return;
}
int mid = (l+r)>>;
if(I <= mid){
upd(l,mid,x<<);
}else{
upd(mid+,r,x<<|);
}
seg[x] = max(seg[x<<],seg[x<<|]);
}
void conit(int x){ //用掉的值
if(!con[x]){
con[x] = ;
I = x;
upd(,n,);
}
}
int ans[MAXN];
int main(){
// freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
cuts.clear();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
ord[a[i]] = i;
con[i] = cut[i] = ;
}
con[n+] = cut[n+] = ;
build(,n,);
cutit(); //加
cutit(n+);//加括号
for(int k = ;k<=n;k++){
int i = ord[k];
int r = -inf;
if(!cut[i+]){ //下一个元素的值
r = a[i+];
}
int l = -inf;
if(!con[i+]){ //左边最靠近i位置的括号的坐标
/*cuts 中存放的是左括号和右括号的坐标
*cuts中寻找已知括号
*/
set<int>::iterator it = cuts.lower_bound(i+);
it--;
l1 = *it; //线段树的左边界应该是从距离i位置最近的括号开始
r1 = i;//线段树的有边界是 i
v = -inf;
que(,n,);//寻找最大值
l = v;
}
if(r > l){ //如果不能插入括号
ans[k] = r;
conit(i+); //把i+1的值消除掉影响
}else{ // 如果能插入括号
ans[k] = l;//则结果为最邻近括号的最大值
int pos = ord[l];
cutit(pos);
cutit(i+);
for(int j = pos+;j <= i;j++){//消除掉影响,后面的则指向下一个
conit(j);
}
}
}
for(int i = ;i<= n;i++){
if(i > ){
printf(" ");
}
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}

hdu5338 ZZX and Permutations的更多相关文章

  1. hdu5338 ZZX and Permutations(贪心、线段树)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud ZZX and Permutations Time Limit: 6000/300 ...

  2. hdu 5338 ZZX and Permutations (贪心+线段树+二分)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  3. 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations

    题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...

  4. HDU 5338 ZZX AND PERMUTATIONS 线段树

    pid=5338" target="_blank" style="text-decoration:none; color:rgb(45,125,94); bac ...

  5. 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 ...

  6. HDU 5338(ZZX and Permutations-用线段树贪心)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  7. 2015 Multi-University Training Contest 4

    1001 Olympiad 签到题1. # include <iostream> # include <cstdio> using namespace std; ]={}; b ...

  8. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. js 选择器

    a>b  获取a下面的直接子元素

  2. Android 一个改进的okHttp封装库

    一.概述 之前写了篇Android OkHttp完全解析 是时候来了解OkHttp了,其实主要是作为okhttp的普及文章,当然里面也简单封装了工具类,没想到关注和使用的人还挺多的,由于这股热情,该工 ...

  3. 转:STL使用入门( Using STL)

    1 介绍 我最开始结束C++编程是从DOS下的Borland C++开始的.那时他们在最新版本3.1中就包含了一套模板库用来做collection.那真是个好东东.当我开始使用Visual C++ 2 ...

  4. perl学习(4) 子程序

    子程序,类比c语言中的函数,在形式上个人认为最大的区别:没有形参 1.1.定义子程序 1.2.调用 #! /usr/bin/perl sub marine { $n += 1 ; print &quo ...

  5. 小程序员在android移动应用上的赚钱经历

    先说说我自己吧,二线城市(以外包为主)的小程序员,工作多年了,月收入5-6K.主要从事asp.net web网站开发,java,c++,php,ruby都懂一些,属于那种对问题不求甚解型,爱好电脑游戏 ...

  6. Ajax学习(三)——XMLHttpRequest对象的五步使使用方法

        Ajax的核心技术是XMLHttpRequest对象,它能够在不向server提交整个页面的情况下.实现局部更新网页.通过这个对象,Ajax能够像桌面应用程序那样仅仅与server进行数据层的 ...

  7. Hibernate级联操作和载入机制(二) cascade and fetch

    上一篇介绍了Hibernate持久化对象时候的级联操作.本篇介绍读取时候的级联操作. 还是用上一篇的样例.一份问卷有多个问题.可是每一个问题仅仅能属于一份问卷. 我们先看測试用例: @Test pub ...

  8. UML 顺序图

    顺序图 顺序图是交互图的一种形式,它显示对象沿生命线发展,对象之间随时间的交互表示为从源生命线指向目标生命线的消息.顺序图能很好地显示那些对象与其它那些对象通信,什么消息触发了这些通信,顺序图不能很好 ...

  9. .Net Core 环境搭建

    .Net Core 系列:1.环境搭建 前言: 2016年6月28日微软宣布发布 .NET Core 1.0.ASP.NET Core 1.0 和 Entity Framework Core 1.0. ...

  10. linux chmod权限

    Linux chmod 命令 chmod用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一种是包含 字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. 1 ...