1178: [Apio2009]CONVENTION会议中心
1178: [Apio2009]CONVENTION会议中心
https://lydsy.com/JudgeOnline/problem.php?id=1178
分析:
set+倍增。
首先把所有有包含的去掉,只保留包含的最小的边(如果两条线段中的一条包含另一条,那么保留被包含的)然后此时就可以直接贪心了。直接从一条边找不想交的下一条边。然后就行了(因为此时没有包含的,左端点递增,右端点递增)。
因为每条边的下一条是唯一的,那么可以倍增维护往后走2^i步,到的点。此时可以快速知道任意一段区间的最多可以有多少条边了。
因为要字典序最小,从编号小的可以是枚举,如果这条边[l,r]可以加进去。tl,tr如下图所示。

那么[l,r]可以加入的条件是,calc(tl+1,tr-1)=calc(tl+1,l-1)+calc(r+1,tr-1)+1,calc(l,r)表示l~r最多可以放几条线段。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const int INF = 1e9;
const int Log = ; struct Edge{
int l, r;
Edge() { }
Edge(int a,int b) { l = a, r = b; }
bool operator < (const Edge &A) const {
return r == A.r ? l > A.l : r < A.r;
}
}A[N], ori[N];
int disc[N << ], f[N][Log + ], X[N], Y[N], m = ;
set<Edge>s; int Calc(int l,int r) {
int p = lower_bound(X + , X + m + , l) - X, ans = ; // ans=1!!!
if (Y[p] > r || p > m) return ;
for (int i = Log; i >= ; --i)
if (f[p][i] && Y[f[p][i]] <= r)
ans += ( << i), p = f[p][i];
return ans;
} int main() {
int n = read();
for (int i = ; i <= n; ++i) {
A[i].l = read(), A[i].r = read();
disc[i] = A[i].l, disc[i + n] = A[i].r;
}
sort(disc + , disc + n + n + );
int cnt = ;
for (int i = ; i <= n + n; ++i) if (disc[i] != disc[cnt]) disc[++cnt] = disc[i];
for (int i = ; i <= n; ++i) {
A[i].l = lower_bound(disc + , disc + cnt + , A[i].l) - disc;
A[i].r = lower_bound(disc + , disc + cnt + , A[i].r) - disc;
ori[i] = A[i];
}
sort(A + , A + n + );
X[m] = A[m].l, Y[m] = A[m].r;
for (int i = ; i <= n; ++i)
if (A[i].l > A[m].l) A[++m] = A[i], X[m] = A[m].l, Y[m] = A[m].r;
for (int i = , j = ; i <= m; ++i) {
while (j <= m && A[j].l <= A[i].r) j ++;
if (j <= m) f[i][] = j;
}
for (int j = ; j <= Log; ++j)
for (int i = ; i <= m; ++i) f[i][j] = f[f[i][j - ]][j - ]; int ans = Calc(-INF, INF);
cout << ans << "\n"; s.insert(Edge(INF, INF));
s.insert(Edge(-INF, -INF)); int now = ;
for (int i = ; i <= n; ++i) {
set<Edge> :: iterator x = s.lower_bound(ori[i]), y = x; y --; // 端点没有重复的,可以直接set的lower_bound
int tl = y->r, tr = x->l, l = ori[i].l, r = ori[i].r;
if (tl >= l || tr <= r) continue;
if (Calc(tl + , tr - ) == Calc(tl + , l - ) + Calc(r + , tr - ) + ) {
if (++now == ans) return printf("%d",i), ;
else printf("%d ",i);
s.insert(ori[i]);
}
}
return ;
}
1178: [Apio2009]CONVENTION会议中心的更多相关文章
- bzoj 1178 [Apio2009]CONVENTION会议中心
这题好难啊! 我好菜啊! 思路:对于最多线段不相交, 我们可以按左端点sort之后,贪心取. 但是这个题要求选取的线段排序之后序号的字典序最小. 那么我们如果按序号贪心地从大往小往里放, 那么对于第k ...
- bzoj 1178: [Apio2009]CONVENTION会议中心(少见做法掉落!)【贪心+二分】
数组若干+手动二分一个的算法,bzoj rank8 ===============================废话分割线=================================== 我我 ...
- bzoj1178 [Apio2009]CONVENTION会议中心 区间dp+贪心
[Apio2009]CONVENTION会议中心 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1130 Solved: 444[Submit][S ...
- BZOJ1178 [Apio2009]CONVENTION会议中心
本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! Description Siruseri政府建造了 ...
- 【bzoj1178】 Apio2009—CONVENTION会议中心
http://www.lydsy.com/JudgeOnline/problem.php?id=1178 (题目链接) 题意 给出n个区间,问在区间两两不相交的情况下最多能选出多少区间,并输出字典序最 ...
- BZOJ1178 [Apio2009]CONVENTION会议中心 贪心 set
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1178 题意概括 一堆线段,现在取出最多条数,使其互不覆盖,并输出字典序最小的方案. 题解 这题好坑 ...
- 【BZOJ】【1178】【APIO2009】convention会议中心
贪心 如果不考虑字典序的话,直接按右端点排序,能选就选,就可以算出ans…… 但是要算一个字典序最小的解就比较蛋疼了= = Orz了zyf的题解 就是按字典序从小到大依次枚举,在不改变答案的情况下,能 ...
- 【BZOJ-1178】CONVENTION会议中心 倍增 + set (神思路好题!)
1178: [Apio2009]CONVENTION会议中心 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 812 Solved: 323[Subm ...
- 【BZOJ 1178】【APIO 2009】CONVENTION会议中心
http://www.lydsy.com/JudgeOnline/problem.php?id=1178 这道题想了好久没想明白,倍增数组通过看题解很快就明白了,但是一小段区间内应有的最多线段数一直不 ...
随机推荐
- 108.UIView关于布局和约束的方法(AutoLayout)
http://blog.csdn.net/wangyanchang21/article/details/52270136 关于布局(UIViewHierarchy) 1.layoutSubviews ...
- MacBook搭建go语言开发环境
mac下要安装 go 最简单的方式是通过 homebrew 直接执行: brew update && brew upgrade brew install go 安装完成后需要指定 GO ...
- gdbt与adboost(或者说boosting)区别
boosting 是一种将弱分类器转化为强分类器的方法统称,而adaboost是其中的一种,或者说AdaBoost是Boosting算法框架中的一种实现 https://www.zhihu.com/q ...
- array和matrix
array:数组 matrix:矩阵 list:列表 a = [[1,2,3],[4,5,6]] 两种array的定义方式,第一种方式可以看出list不是array,但却有很大的联系 a = np.a ...
- Java语言实现通过Ajax抓取后台数据及图片
1.Java语言实现通过Ajax抓取后台数据及图片信息 1.1数据库设计: create table picture( pic_id number not null, pic_name )not nu ...
- IQueryable和IEnumerable
使用EF你必须知道这两个的区别,可以帮助我们的提升性能. 表达树:Linq 表达 ①IQueryable和IEnumerable IQueryable 延时执行:扩展方法接受的是Expression( ...
- 两个事务 update同一张表出现的死锁问题 (转载)
引言 近来做省一级计算机一级考试系统的时候,学生端进行大批量判分的时候,出现了这样的问题(事务(进程 ID 262)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品.请重新运行该事务.): 这 ...
- gdb tui中切换窗口
gdb的gui用法 调试代码的时候,只能看到下一行,每次使用list非常烦,不知道当前代码的context http://beej.us/guide/bggdb/#compiling 简单来说就是在 ...
- vue中监听页面滚动和监听某元素滚动
①监听页面滚动 在生命周期mounted中进行监听滚动: mounted () { window.addEventListener('scroll', this.scrollToTop) }, 在方法 ...
- iOS:cocoapods 配置相关(19-04-02更)
1.gem sources 2.libwebp 1.gem sources 因为,mac更新,cocoapods也要更新,使用下面指令,提示找不到.org,原因是淘宝的镜像源.org换成.com,所以 ...