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. Qt 技巧: 解决未解析的SSL问题

    因为https访问需要用到SSL认证,而QT默认是不支持SSL认证,所以在使用之前必须先做一些准备工作: 需要安装OpenSSL库: 1.首先打开http://slproweb.com/product ...

  2. github的.md格式文件

    md文件是github改良了markdown的语法,用来显示在项目首页的文件.在官方的网址说的很清楚: GitHub uses what we're calling "GitHub Flav ...

  3. 基于Bresenham算法画圆

    bresenham算法画圆思想与上篇 bresenham算法画线段 思想是一致的 画圆x^2+y^2=R^2 将他分为8个部分,如上图 1. 只要画出1中1/8圆的圆周,剩下的就可以通过对称关系画出这 ...

  4. HDU2276 - Kiki &amp; Little Kiki 2(矩阵高速幂)

    pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...

  5. jvm Classload method介绍

    1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...

  6. 利用VC助手(VA)添加注释

    利用VC助手(VA)添加注释 今天想给自己写的代码加上版权信息,同时整理一下代码的注释.但是为了保持同样的格式,总是copy,显得有些繁琐.然后试图找解决方案.我用的是VS 2010, 刚开始是尝试了 ...

  7. Checkbox in DataList

    一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...

  8. Android线程和handler

    根据视频仿照着写了个demo: package com.wyl.wylthreadtest; import android.graphics.Color; import android.os.Bund ...

  9. <转> 30 个有关 Python 的小技巧

    目录[+] 1.1 拆箱 1.2 拆箱变量交换 1.3 扩展拆箱(只兼容python3) 1.4 负数索引 1.5 切割列表 1.6 负数索引切割列表 1.7指定步长切割列表 1.8 负数步长切割列表 ...

  10. 第三届蓝桥杯 c/c++真题

    第三届蓝桥杯真题 c/c++ 以下题目我自己也并不是所有的题目都是一次性就能做对或是有结题思路的.有些题目也是经过查证网上相关的资料或是参考了别人的代码和解题思路才做出来的.总的来看,这份题目考了很多 ...