1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include<bits/stdc++.h> #define ll long long #define fi first #define se second #define pb push_back using namespace std; const int N=305; int n,cnt,lsh[N],f[N][N]; struct node{ int p,l,r; bool operator <(const node &a)const{ return p<a.p; } }q[N]; int get(int x){ return lower_bound(lsh+1,lsh+1+cnt,x)-lsh; } void add(int &x,int y){ x=max(x,y); } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ int a,l;cin>>a>>l; q[i]={a,a-l,a+l},lsh[++cnt]=a,lsh[++cnt]=a-l,lsh[++cnt]=a+l; }sort(lsh+1,lsh+1+cnt),cnt=unique(lsh+1,lsh+1+cnt)-lsh-1; for(int i=1;i<=n;i++){ q[i].p=get(q[i].p),q[i].l=get(q[i].l),q[i].r=get(q[i].r); }sort(q+1,q+1+n); for(int i=1;i<=n;i++){ int p=q[i].p,l=q[i].l,r=q[i].r; for(int k=p+1;k<=r;k++)add(f[i][k],f[i-1][p]+lsh[k]-lsh[p]); int j=i-1,mx=p; for(int k=l;k<=cnt;k++){ while(j&&mx<k)mx=max(mx,q[j].r),j--; if(mx>=k)add(f[i][k],f[j][l]+lsh[k]-lsh[l]); } for(int j=1;j<=cnt;j++)add(f[i][j],f[i][j-1]),add(f[i][j],f[i-1][j]); } cout<<f[n][cnt]; }
|