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. QNetworkAccessManager跳转URL处理(使用QNetworkRequest::RedirectionTargetAttribute获得跳转URL)

    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); void Mai ...

  2. JRebel 6 破解版及使用方法

    最近更新到jrebel6.2.1了,我自己做了个技术分享的微信公众号(茶爸爸),有心的朋友可以来这里一起学习 云盘下载链接: http://pan.baidu.com/s/1bnGzMUF 配置: - ...

  3. java学习之xml

    xml的处理有两种方式dom和Sax 其中dom有3套api ,分别是dom和jdom和dom4j package com.gh.xml; import java.io.File; import ja ...

  4. CocoaPods的安装及设置

    1>CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具 在开发iOS应用时,会经常使用第三方类库,手动下载比较麻烦,通过CocoaPods可以便捷的下载与管理第三 ...

  5. Swift--基本数据类型(一)

    不像更多语言中,X不要求你写一个分号(;)在你的代码中的每一个语句后,尽管能够这样做.然而,假设你想在一行中写入多个单独的语句分号是必需的: .    1  let cat = "" ...

  6. 10994 - Simple Addition(规律)

    Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...

  7. codeigniter ,看完这些,就可以用它做项目了

    一.MVC 1,入口文件 唯一一个让浏览器直接请求的脚本文件 2,控制器 controller 负责协调模型和视图 3,模型 model 只负责提供数据,保存数据 4,视图 只负责显示,以及搜集用户的 ...

  8. PyQt中如何隐藏Menu

    PyQt中隐藏一个Menu Item,可以通过QAction的setVisible(False)来设置,而QMenu的setVisible(False)是不管用的. 现在问题来了,我们有一个菜单,它有 ...

  9. Qt 操作 pdf 文件

    写了好久的东西,不小心按了下返回键就没了.CSDN居然没自动保存,坑爹啊 原本还有很多信息的,现在直入正题吧. QT没有内置PDF操作的功能(其实有一个,QPrinter,不过只能写不能读,基本是半残 ...

  10. php language construct 语言构造器

    isset和empty看起来像是函数,我们也经常把它当作函数一样使用,但是实际上,它们是语言构造器. php中的语言构造器就相当于C中的预定义宏的意思,它属于php语言内部定义的关键词,不可以被修改, ...