题目链接 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. Python中关于函数的介绍

    一.什么是函数       当我们在日常工作中编写代码时,有没有发现这种情况,写了一套代码,却发现里面有很多段代码出现了有规律的重复,这样就不符合一个合格程序员的标准了,一个合格的程序员编写的代码最重 ...

  2. Altium Designer入门学习笔记2:使用原创客3D元件库

    请自行淘宝购买: 元件库列表(2018年11月27日): 问题一:在项目库或已安装的库中找不到? 将"原创客"提供的文件全部添加到libraries中!"原创客" ...

  3. for_each_node(node)

    遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...

  4. 思维水题:UVa512-Spreadsheet Tracking

    Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...

  5. python基础学习笔记——反射

    对编程语言比较熟悉的朋友,应该知道“反射”这个机制.Python作为一门动态语言,当然不会缺少这一重要功能.然而,在网络上却很少见到有详细或者深刻的剖析论文.下面结合一个web路由的实例来阐述pyth ...

  6. loj2182 「SDOI2015」寻宝游戏

    参考这里 #include <iostream> #include <cstdio> #include <set> using namespace std; typ ...

  7. luogu2698 [USACO12MAR]花盆Flowerpot

    单调队列+二分答案 #include <algorithm> #include <iostream> #include <cstring> #include < ...

  8. ant 入门级详解

    ant 入门级详解   [转载的地址(也是转载,未找到原文地址)]https://www.cnblogs.com/jsfx/p/6233645.html 1,什么是antant是构建工具2,什么是构建 ...

  9. python - work5 - 类与对象

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: work_20181119.py@ide: PyCharm Communit ...

  10. ccna 闫辉单臂路由 和 acl access control list

    ccna 闫辉单臂路由 和  acl   access control list 一单臂路由     当前园区网设计很少用到       成本低  小型的.局域网可用         二ACL acc ...