clique
10.2
思路:
数据结构维护dp
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 200010
#define inf 1000000010
#define LL long long
using namespace std;
int n, ans = 0;
int f[N], c[N], vis[N];
LL b[N];
struct AA{
LL x, w;
}a[N];
bool cmp(AA a, AA b){
return a.x < b.x;
}
int query_pos(LL x){
return upper_bound(b+1,b+n+1,x)-(b+1);
}
void modify(int x, int val){
if(x == 0) return ;
for(int i=x; i<=n; i+=(i & (-i)))
c[i] = max(c[i], val);
}
int query(int x){
int rt = 0;
for(int i=x; i; i-=(i & (-i)))
rt = max(rt, c[i]);
return rt;
}
int main(){
freopen ("clique.in", "r", stdin);
freopen ("clique.out", "w", stdout);
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%I64d%I64d", &a[i].x, &a[i].w);
b[i] = a[i].x + a[i].w;
}
sort(a+1, a+n+1, cmp);
sort(b+1, b+n+1);
b[0] = - inf;
for(int i=1; i<=n; i++){
int cc = query_pos(a[i].x - a[i].w);
f[i] = query( cc ) + 1;
cc = query_pos(a[i].x + a[i].w);
while( vis[cc] ) cc--;
vis[cc] = 1;
modify(cc, f[i]);
}
for(int i=1; i<=n; i++)
ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}