题目链接 Restaurant

题目意思就是在$n$个区间内选出尽可能多的区间,使得这些区间互不相交。

我们先对这$n$个区间去重。

假如有两个区间$[l1, r1],[l2, r2]$

若满足$l1 >= l2$且 $r1 <= r2$,那么$[l2, r2]$就是可以被去掉的。

因为这两个区间里我们显然最多只能选择一个。

如果我们在答案里选择了$[l2, r2]$,那么我们如果把$[l2, r2]$换成$[l1, r1]$的话

这个答案肯定还是满足题意的。

甚至可能腾出了可以放下其他区间的空间。

那么我们去重之后进行离散化,接下来就是贪心的过程。

我们把这些处理好的区间以左端点为关键字升序排序。

排好序的区间,右端点肯定也是升序的。

我们预处理出$f[i]$为右端点小于等于$i$的所有区间的编号的最大值。

我们从最后一个区间开始选,

对于当前我们可以选择的范围肯定要选择编号更大的。

因为编号越大说明左端点越大,给其他区间留出的空间越多。

于是我们从最后一个区间开始选,依次贪心,这样就可以了。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int inf = 1e9 + 1;
const int N = 1e6 + 10;
const int A = 22; struct node{
int x, y, s, t;
friend bool operator < (const node &a, const node &b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
} a[N], c[N]; int b[N << 2], d[N << 2];
int cnt, tot, now;
int n, m, et;
int mx;
int st[N << 1][A];
int ans; bool cmp(const node &a, const node &b){
return a.s == b.s ? a.t > b.t : a.s < b.s;
} int main(){ scanf("%d", &n);
cnt = 0;
rep(i, 1, n){
int x, y;
scanf("%d%d", &a[i].x, &a[i].y);
a[i].s = a[i].y;
a[i].t = a[i].x + inf;
} sort(a + 1, a + n + 1, cmp); cnt = 0;
for (int i = 1, j; i <= n;){
j = i + 1;
while (j <= n && a[j].t <= a[i].t) ++j;
c[++cnt] = a[i];
i = j;
} et = 0;
rep(i, 1, cnt){
b[++et] = c[i].x;
b[++et] = c[i].y;
} rep(i, 1, et) d[i] = b[i];
sort(d + 1, d + et + 1);
tot = unique(d + 1, d + et + 1) - d - 1;
rep(i, 1, et) b[i] = lower_bound(d + 1, d + tot + 1, b[i]) - d;
et = 0;
rep(i, 1, cnt){
c[i].x = b[++et];
c[i].y = b[++et];
} mx = c[cnt].y; rep(i, 1, mx){
if (i < c[1].y) continue;
int l = 1, r = cnt;
while (l + 1 < r){
int mid = (l + r) >> 1;
if (c[mid].y <= i) l = mid;
else r = mid - 1;
} if (c[r].y <= i) st[i][0] = r;
else st[i][0] = l; } now = mx;
ans = 0;
for (; now > 0; ){
int fl = st[now][0];
if (fl == 0) break;
++ans;
now = c[fl].x - 1;
} printf("%d\n", ans);
return 0; }

  

Codeforces 597B Restaurant(离散化 + 贪心)的更多相关文章

  1. codeforces 597B Restaurant

    题目链接:http://codeforces.com/contest/597/problem/B 题目分类:贪心 题目分析:经典的看节目问题(挑战程序设计page 40) 代码: #include&l ...

  2. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  3. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  4. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  5. Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)

    题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...

  6. Codeforces Round #545 (Div. 2) C. Skyscrapers 离散化+贪心

    题目链接 给你一个n∗m的矩阵res,让你输出一个n∗m的矩阵a,这个矩阵满足:给你一个n*m的矩阵res,让你输出一个n*m的矩阵a,这个矩阵满足:给你一个n∗m的矩阵res,让你输出一个n∗m的矩 ...

  7. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  8. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  9. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

随机推荐

  1. destoon 后台管理左侧新增菜单项

    destoon 后台菜单设置在对应模块的admin/menu.inc.php 例如要在后台会员管理里增加会员承包和股东管理 $menu = array( array('添加会员', '?modulei ...

  2. leetcode-26-exercise_linked-list

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. 解题思路: 需要检查before和afte ...

  3. LeetCode(143) Reorder List

    题目 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...

  4. 《Scrum实战》第2次课【取得大家的支持】课后作业汇总

    作业:<变革之心>读后感 孟帅: 2017-7-12http://www.cnblogs.com/mengshuai1982/p/7153985.html

  5. Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决

    多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spri ...

  6. Linux 安装 tree命令

    通过yum在线安装tree包 yum install tree -y

  7. python-网络编程-02

    [1] server端 首先我们看下一个最简单http服务端 import socket def handle_request(client): buf = client.recv(1024) cli ...

  8. 【185天】黑马程序员27天视频学习笔记【Day14-下】

    叨逼叨两句 不容易,白天被叫去帮忙,不得已晚上来挑灯夜战,熬到2点,总算完成任务了. 我打算下周开始换一个更新时间,每次把deadline设置为晚上12点,都会接近或者超过这个时间,之后改成中午12点 ...

  9. hdu3667

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. HDU——1405The Last Practice(试手map)

    The Last Practice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...