这题好难啊! 我好菜啊!

思路:对于最多线段不相交, 我们可以按左端点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. 洛谷 P5108 仰望半月的夜空 解题报告

    P5108 仰望半月的夜空 题目描述 半月的夜空中,寄托了多少人与人之间的思念啊 曦月知道,这些思念会汇集成一个字符串\(S(n = |S|)\) 由于思念汇集的过于复杂,因此曦月希望提炼出所有的思念 ...

  2. Eclipse配置Maven的一些问题

    问题1:连接私服 build项目非常缓慢 配置好本地的setting文件后,发现build非常缓慢,照显示的进度,可能要一天才会build后一个项目,同事指导解决方法如下: MyEclipse2017 ...

  3. python---django使用cookie和session

    在views中的调用: def login(req): message='' if req.method == "POST": user = req.POST.get(" ...

  4. shell jq

    Mark 下,周末来补充 参考资料: https://stedolan.github.io/jq/tutorial/

  5. bzoj千题计划287:bzoj1228: [SDOI2009]E&D

    http://www.lydsy.com/JudgeOnline/problem.php?id=1228 打SG函数表,找规律: 若n是奇数m是奇数,则SG(n,m)=0 若n是偶数m是偶数,则SG( ...

  6. bzoj千题计划256:bzoj2194: 快速傅立叶之二

    http://www.lydsy.com/JudgeOnline/problem.php?id=2194 相乘两项的下标 的 差相同 那么把某一个反过来就是卷积形式 fft优化 #include< ...

  7. Redis 学习小记

    由于是学习笔记,我就不来各种啰嗦,介绍这个介绍那个,也不上交给国家,或者各种对比,相信如果你真心用 redis 的话,就不会去跟 MySql,Memcached,MongoDB 等做对比了. 我原先用 ...

  8. 总有一些实用javascript的元素被人遗忘在角落-slice

    slice() 方法可从已有的数组中返回选定的元素. 好吧,我承认我竟然把它忘了! 这次我在回顾一下它 语法 arrayObject.slice(start,end) 数组.slice(起始,结束) ...

  9. svn使用笔记

    一.checkout:第一次下载trunk里面的代码到本地 二.commit:提交一些修改* out of date : 本地版本号 < 服务器版本号* 如果过期,就update,可能会出现co ...

  10. Export SQLite data to Excel in iOS programmatically(OC)

    //For the app I have that did this, the SQLite data was fairly large. Therefore, I used a background ...