COGS 2086 - 平凡的题面 (贪心)
## 题意 ##
有n个点,m个区间,每个区间只能包含一个点。问最多能包含几个点。
## 思路 ##
对于某个点,选择右端点尽量靠左的最优。这样并不会使结果变得更差。
一开始我把区间全部放到优先队列里了,这样就导致了当右端点足够,左端点不够的时候就不知道该不该抛弃这个区间。
正确的应该是扫描全部区间,区间按左端点排序,到了就添加。这样就保证了优先队列里的左端点都是符合的,那么只要判断右端点就行。
## 代码 ##
#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e5+10;
const int MOD = 100000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef std::pair<int, int> pii;
struct cmp
{
bool operator () (const pii a, const pii b)
{
return a.Y > b.Y;
}
};
priority_queue<pii, vector<pii>, cmp> Q;
int arr[MAXN];
pii p[MAXN];
int main()
{
// ROP;
freopen("bg.in", "r", stdin);
freopen("bg.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
for (int i = 0; i < m; i++) scanf("%d%d", &p[i].X, &p[i].Y);
sort(arr, arr+n);
sort(p, p+m);
int ppos = 0, arr_pos = 0;
int ans = 0;
for (int i = 1; i < MAXN; i++)
{
while (p[ppos].X == i)
{
Q.push(p[ppos]);
++ppos;
}
while (arr[arr_pos] == i)
{
while (!Q.empty() && Q.top().Y < i) Q.pop();
if (!Q.empty())
{
++ans;
Q.pop();
}
++arr_pos;
}
}
printf("%d\n", ans);
return 0;
}