这题好难啊! 我好菜啊!

思路:对于最多线段不相交, 我们可以按左端点sort之后,贪心取。 但是这个题要求选取的线段排序之后序号的字典序最小。

那么我们如果按序号贪心地从大往小往里放, 那么对于第k个线段,我们考虑放进去之后是能是还能保证所取的线段个数能

达到最大, 我们考虑函数cal(l, r) 表示坐标轴上l 到 r 最多能选取多少线段,第k条线段的左右端点是l, r, 它左边第一条是l1, r1

它右边第一条是l2, r2, 那么k能放进去的充分必要条件就是cal(r1 + 1, l2 - 1) == cal(r1 + 1, l - 1) + cal(r + 1, l2 - 1) + 1。

那么我们的问题就变成了cal()这个函数如何实现,我们将原来的线段按右端点排序之后,把包含其他线段的线段全部删掉,

然后nx[ i ][ 0 ] = k 表示 k是第一个满足b[k].l > b[i].r 的线段, 然后我们再倍增一下,求出nx[ i ][ j ]表示 i 右边第2^j 个线段是谁。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int>> using namespace std; const int N=+;
const int M=1e4+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9 + ; int n, m, nx[N][], L[N], R[N]; struct Line {
int l, r; Line(int _l = , int _r = ) {
l = _l;
r = _r;
}
bool operator < (const Line &rhs) const {
if(r == rhs.r) return l < rhs.l;
return r < rhs.r;
} } a[N], b[N]; int cal(int l, int r) {
int x = lower_bound(L + , L + + m, l) - L;
if(x > m || R[x] > r) return ;
int ans = ;
for(int i = ; i >= ; i--) {
if(nx[x][i] && R[nx[x][i]] <= r) {
ans += << i;
x = nx[x][i];
}
}
return ans;
}
int main() { scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d%d", &a[i].l, &a[i].r);
b[i] = a[i];
} sort(b + , b + n + ); for(int i = ; i <= n; i++) {
if(!m || b[i].l > b[m].l)
b[++m] = b[i];
} for(int i = ; i <= m; i++)
L[i] = b[i].l, R[i] = b[i].r; for(int i = , j = ; i <= m; i++) {
while(j <= m && b[j].l <= b[i].r) j++;
if(j <= m) nx[i][] = j;
} for(int i = ; i <= ; i++) {
for(int j = ; j <= m; j++) {
nx[j][i] = nx[nx[j][i - ]][i - ];
}
} int ans = cal(-inf, inf);
printf("%d\n", ans);
set<Line> st;
int cnt = ;
st.insert(Line(inf, inf));
st.insert(Line(-inf, -inf)); for(int i = ; i <= n; i++) {
set<Line>::iterator it = st.lower_bound(a[i]), itt = it; itt--;
int l1 = itt -> r, r1 = a[i].l, l2 = a[i].r, r2 = it -> l;
if(l1 >= r1 || l2 >= r2) continue;
if(cal(l1 + , r2 - ) == cal(l1 + , r1 - ) + cal(l2 + , r2 - ) + ) {
if(++cnt == ans) printf("%d", i);
else printf("%d ", i);
st.insert(a[i]);
}
}
puts("");
return ;
}
/*
*/

bzoj 1178 [Apio2009]CONVENTION会议中心的更多相关文章

  1. bzoj 1178: [Apio2009]CONVENTION会议中心(少见做法掉落!)【贪心+二分】

    数组若干+手动二分一个的算法,bzoj rank8 ===============================废话分割线=================================== 我我 ...

  2. 1178: [Apio2009]CONVENTION会议中心

    1178: [Apio2009]CONVENTION会议中心 https://lydsy.com/JudgeOnline/problem.php?id=1178 分析: set+倍增. 首先把所有有包 ...

  3. bzoj1178 [Apio2009]CONVENTION会议中心 区间dp+贪心

    [Apio2009]CONVENTION会议中心 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1130  Solved: 444[Submit][S ...

  4. BZOJ1178 [Apio2009]CONVENTION会议中心

    本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! Description Siruseri政府建造了 ...

  5. 【bzoj1178】 Apio2009—CONVENTION会议中心

    http://www.lydsy.com/JudgeOnline/problem.php?id=1178 (题目链接) 题意 给出n个区间,问在区间两两不相交的情况下最多能选出多少区间,并输出字典序最 ...

  6. BZOJ1178 [Apio2009]CONVENTION会议中心 贪心 set

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1178 题意概括 一堆线段,现在取出最多条数,使其互不覆盖,并输出字典序最小的方案. 题解 这题好坑 ...

  7. 【BZOJ】【1178】【APIO2009】convention会议中心

    贪心 如果不考虑字典序的话,直接按右端点排序,能选就选,就可以算出ans…… 但是要算一个字典序最小的解就比较蛋疼了= = Orz了zyf的题解 就是按字典序从小到大依次枚举,在不改变答案的情况下,能 ...

  8. 【BZOJ-1178】CONVENTION会议中心 倍增 + set (神思路好题!)

    1178: [Apio2009]CONVENTION会议中心 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 812  Solved: 323[Subm ...

  9. 【BZOJ 1178】【APIO 2009】CONVENTION会议中心

    http://www.lydsy.com/JudgeOnline/problem.php?id=1178 这道题想了好久没想明白,倍增数组通过看题解很快就明白了,但是一小段区间内应有的最多线段数一直不 ...

随机推荐

  1. 51nod1462 树据结构(树链剖分+线段树)

    这题好久之前就被学长安利了...一直没写珍藏在收藏夹一个不为人知的角落233 这题怎么做...我们来数形结合,横坐标为$t_i$被加的次数(可看作时间$t$),纵坐标为$v_i$,那么$t_i$实际上 ...

  2. webService与分布式与微服务与SOA的关系

    SOA:是面向服务体系架构. webservice是SOA的一种实现技术.webservice基于两种协议:soap和rest协议.现在常用的是rest协议. web service (web 服务) ...

  3. HTML5 快速学习二 Canvas

    本篇文章开始讲解HTML5的核心功能之一:Canvas 通过Canvas可以动态生成和展示图形.图表.图像以及动画. Canvas API功能非常多,我们将讨论最常用的功能. 我们先新建一个canva ...

  4. JS中的继承链

    我们首先定义一个构造函数Person,然后定义一个对象p,JS代码如下: function Person(name) { this.name = name; } var p = new Person( ...

  5. 【1】ConcurrentModificationException 异常解析和快速失败,安全失败

    目录 一.引起异常的代码 二.foreach原理 三.从ArrayList源码找原因 四.单线程解决方案 五.在多线程环境下的解决方法 一.引起异常的代码 以下三种的遍历集合对象时候,执行集合的rem ...

  6. Linux遇到的问题(一)Ubuntu报“xxx is not in the sudoers file.This incident will be reported” 错误解决方法

    提示错误信息 www@iZ236j3sofdZ:~$ ifconfig Command 'ifconfig' is available in '/sbin/ifconfig' The command ...

  7. python学习笔记6--操作Mysql

    一.mysql操作 import pymysql #连上mysql ip 端口号 密码 账号 数据库 #建立游标 #执行sql #获取结果 #关闭连接.关闭游标 conn=pymysql.connec ...

  8. Struts2_day02

    一.内容大纲 1 结果页面配置 (1)全局结果页面 (2)局部结果页面 - 配置全局也配置局部,最终局部为准 (3)result标签type属性 - 默认值 dispatcher做转发 - redir ...

  9. 20155237 2016-2017-2 《Java程序设计》第7周学习总结

    20155237 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 认识Lambda语法 Lambda 教材的引入循序渐近.深入浅出 Lambda去重复,回忆D ...

  10. Route Between Two Nodes in Graph

    Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...