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会议中心的更多相关文章

  1. bzoj 1178 [Apio2009]CONVENTION会议中心

    这题好难啊! 我好菜啊! 思路:对于最多线段不相交, 我们可以按左端点sort之后,贪心取. 但是这个题要求选取的线段排序之后序号的字典序最小. 那么我们如果按序号贪心地从大往小往里放, 那么对于第k ...

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

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

  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. 面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统

    面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统 原型模式和基于原型继承的JavaScript对象系统 在 Brendan Eich 为 JavaScrip ...

  2. Vue - 如何实现一个双向绑定

    JS - 如何实现一个类似 vue 的双向绑定 Github JS 实现代码 先来看一张图: 这张图我做个简要的描述: 首先创建一个实例对象,分别触发了 compile  解析指令 和 observe ...

  3. Linux下安装PHP并在nginx服务器中进行配置的详细方法

    先介绍一下使用的环境:centos 7.4, PHP 7.0 , nginx 1.12 Linux系统版本可以通过命令:lsb_release -a 查看. 现在开始步入正题了! 1.  首先查看一下 ...

  4. WEB安全 ACCESS 注入、盲注脚本

    http://www.xxx.cn/cp.asp?classid=3http://www.xxx.cn/cp.asp?classid=3 and //有拦截关键字http://www.xxx.cn/c ...

  5. On Java 8中文版 英雄召集令

    这是该项目的GITHUB地址:https://github.com/LingCoder/OnJava8 广招天下英雄,为开源奉献!让我们一起来完成这本书的翻译吧! 如果您在阅读本书的过程中有发现不明白 ...

  6. 理解numpy exp函数

    exp,高等数学里以自然常数e为底的指数函数 Exp:返回e的n次方,e是一个常数为2.71828 Exp 函数 返回 e(自然对数的底)的幂次方.   a = 1 print np.exp(a) a ...

  7. node.js环境下写的vue项目

    github地址:https://github.com/anxizhihai/JournalismProject.git

  8. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  9. 树概念及使用connect by进行级联查询

    树 树,大家都见过,以这种形式的数据关系,就是树.下面看一张图,了解什么是根节点(树干).节点或分叉.叶(叶节点) connect by 级联查询 connect by可以用于级联查询,常用于对具有树 ...

  10. 关于@synchronized 比你想知道的还多

    如果你曾经使用Objective-C做过并发编程,那你肯定见过@synchronized这个结构.@synchronized这个结构发挥了和锁一样的作用:它避免了多个线程同时执行同一段代码.和使用NS ...