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 ...
随机推荐
- 修改SQL Server 排序规则 (未能排它地锁定数据库以执行该操作)
use master go alter database DBName set single_user go alter database DBName COLLATE Chinese_PRC_CI_ ...
- Eclipse 编码区-保护色-快捷大全
Eclipse快捷键大全 Eclipse编码区-保护色-快捷设置 1.设置路径: windows-preferences-general-editors-text Editors-ba ...
- Windows SDK笔记(经典--一定要看)
Windows SDK笔记(一):Windows程序基本结构 一.概述 Windows程序具有相对固定的结构,对编写者而言,不需要书写整个过程,大部分过程由系统完成.程序中只要按一定的格式填写系统留给 ...
- VS2010/MFC对话框一:创建对话框模板和修改对话框属性
创建对话框主要分两大步: 第一,创建对话框资源,主要包括创建新的对话框模板.设置对话框属性和为对话框添加各种控件: 第二,生成对话框类,主要包括新建对话框类.添加控件变量和控件的消息处理函数等. 创建 ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- 【课程分享】深入浅出嵌入式linux系统移植开发 (环境搭建、uboot的移植、嵌入式内核的配置与编译)
深入浅出嵌入式linux系统移植开发 (环境搭建.uboot的移植.嵌入式内核的配置与编译) 亲爱的网友,我这里有套课程想和大家分享,假设对这个课程有兴趣的,能够加我的QQ2059055336和我联系 ...
- hibernate一对多关联关系
想了几天,终于知道sql语句的发出问题.查了很多书,感觉都没有说清楚,有的还是错的.请看下面: <?xml version="1.0"?> <!DOCTYPE h ...
- [Swust OJ 1097]--2014(数位dp)
题目链接:http://acm.swust.edu.cn/problem/1097/ Time limit(ms): 1000 Memory limit(kb): 32768 今年是2014年,所 ...
- .NET通过PowerShell操作ExChange为用户开通邮箱账号
最近工作中一个web项目需要集成exchange邮箱服务,注册用户时需要动态创建邮箱用户,终于在http://www.cnblogs.com/gongguo/archive/2012/03/12/23 ...
- HttpResponseRedirect VS HttpResponse
当我们处理了post提交的数据之后,我们使用HttpResponseRedirect跳转到另一个页面,而不是用HttpResponse. 例如当一个投票环节时使用HttpResponse可以使用浏览器 ...