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的更多相关文章
- hdu5338 ZZX and Permutations(贪心、线段树)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud ZZX and Permutations Time Limit: 6000/300 ...
- 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 5338 ZZX AND PERMUTATIONS 线段树
pid=5338" target="_blank" style="text-decoration:none; color:rgb(45,125,94); bac ...
- 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 ...
- 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
1001 Olympiad 签到题1. # include <iostream> # include <cstdio> using namespace std; ]={}; b ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- 转: requirejs中文api (详细)
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...
- js左侧三级菜单导航代码
效果演示: 实例代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课
题目 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec Memory Limit: 64 MB Description 考虑一 ...
- Linux编程---I/O部分
非常多函数都能够在网上找到,也比較基础,所以原型仅仅给出了函数名.详细用到再man吧. 输入输出是个非常重要的一块内容.差点儿网络相关的东西基本都是靠底层IO调用来实现的. 好吧.还是先踏踏实实的介绍 ...
- RAC Cache Fusion 原理理解
cache fusion . grd . drm . gcs . ges cache fusion 1.RAC是一个数据库执行在多个实例上.通过DLM(Distributed ...
- 【数据库摘要】5_Sql_IN
IN 操作符 IN 操作符同意您在 WHERE 子句中查找多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...
- 一次搞定cocos2d-x的 Android.mk 文件
cocos2d-x 打 android 包时要修改 Android.mk 文件,但每次修改很麻烦,如果源文件很多的话就坑死了,也可以写一个脚本来自动生成这个文件,但每次还要重新生成一下,将其修改下面的 ...
- CSS3中的border-radius
以前在CSS2的基础上做圆角还能算得上是门学问!!各种图片.各种嵌套(<精通CSS——高级web标准解决方案>中有介绍,过程在这就不说了,网上一查就查得到,总之就是:没用CSS3之前觉得很 ...
- 【JavaScript】强制缓存刷新
1.在js引用时加入时间戳. <script> document.write('<script src="xxx.js?_dc='+new Date().getTime() ...
- ios 调用webservice整理
资料地址:http://blog.sina.com.cn/s/blog_a30ee5f701016yn3.html 学iphone开发有一段时间了,对于我这个新手来说,学习过程中,遇到不少问题,尤其是 ...