[Codeforces137C]History(排序,水题)

时间:2023-03-10 04:20:46
[Codeforces137C]History(排序,水题)

题目链接:http://codeforces.com/contest/137/problem/C

题意:给n对数,分别是一个事件的起始和终止时间。问被有几个事件被其他事件包含。

思路:先排序,按照起始时间优先,终止时间次要排。每次维护当前的终止时间。

由于排序,第i+1个的起始时间一定比第i个的起始时间大(题目要求任意两个事件的起始、终止时间不会相等),所以我们往后遍历的时候记住当前位置最大的终止时间,那么后面的事件再拿来看的时候,如果终止时间小于之前最大的终止时间,就一定会被包含在内了。

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Fuint(a) memset((a), 0x7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long Uint;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<int> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Node {
int a, b;
}Node;
const int maxn = ;
int n;
Node e[maxn]; bool cmp(Node x, Node y) {
if(x.a == y.a) return x.b < y.b;
return x.a < y.a;
} int main() {
// FRead();
Rint(n);
Rep(i, n) {
Rint(e[i].a); Rint(e[i].b);
}
sort(e, e+n, cmp);
int hi = ;
int ret = ;
Rep(i, n) {
if(e[i].b < hi) ret++;
hi = max(hi, e[i].b);
}
printf("%d\n", ret);
RT ;
}