洛谷 - P9350
Chthohepta · · 个人记录
题意
JOI王国有
Rie出版了一本信息学书籍。为了鼓励更多人购买,她可以向部分居民捐赠书籍。若向第
- 第
i 位居民与第j 位居民在坐标轴上的距离不超过E_i - E_j 。即满足|X_i − X_j| \le E_i − E_j 。
如果所有居民都阅读了Rie的书籍,信息学奥林匹克将获得广泛认可。请编写程序计算:为了让JOI王国所有居民都获得书籍,最少需要向多少位居民捐赠书籍。
思路
先推式子。
转化一下
再变一下
设
所以
图:
(随手画勿喷)。
图中没有覆盖的点要选。
可以按
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 5e5 + 2;
int n;
struct jm{
int x, e;
int a, b;
} a[N];
int st[N], stc;
int ans;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i].x >> a[i].e;
a[i].a = a[i].x - a[i].e;
a[i].b = a[i].x + a[i].e;
}
sort(a + 1, a + 1 + n, [](const jm & x, const jm & y){ if(x.a == y.a){ return x.b > y.b; } return x.a < y.a; });
for(int i = 1; i <= n; i ++){
///cout << a[i].x << ' ' << a[i].e << ' ' << a[i].a << ' ' << a[i].b << '\n';
}
for(int i = 1; i <= n; i ++){
while(stc > 0 && a[st[stc]].b < a[i].b){
stc --;
}
if(stc == 0){
ans ++;
}
stc ++;
st[stc] = i;
}
cout << ans;
return 0;
}
/***
***/