The main walking trail in Geraldion is absolutely straight, and it passes strictly from the north to the south, it is so long that no one has ever reached its ends in either of the two directions. The Geraldionians love to walk on this path at any time, so the mayor of the city asked the Herald to illuminate this path with a few spotlights. The spotlights have already been delivered to certain places and Gerald will not be able to move them. Each spotlight illuminates a specific segment of the path of the given length, one end of the segment is the location of the spotlight, and it can be directed so that it covers the segment to the south or to the north of spotlight.
The trail contains a monument to the mayor of the island, and although you can walk in either directions from the monument, no spotlight is south of the monument.
You are given the positions of the spotlights and their power. Help Gerald direct all the spotlights so that the total length of the illuminated part of the path is as much as possible.
The first line contains integer n (1 ≤ n ≤ 100) — the number of spotlights. Each of then lines contains two space-separated integers,ai andli (0 ≤ ai ≤ 108,1 ≤ li ≤ 108). Numberai shows how much further thei-th spotlight to the north, and numberli shows the length of the segment it illuminates.
It is guaranteed that all the ai's are distinct.
Print a single integer — the maximum total length of the illuminated part of the path.
3 1 1 2 2 3 3
5
4 1 2 3 3 4 3 6 2
9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DP+神奇的思路~
用f[i][j][k]表示i盏灯,目前覆盖到区间最右面的是第j盏灯,第j盏灯的光是向左/右(0/1)的,这样,区间最右面的坐标就是a[j].x+a[j].l*k。
然后,枚举右面的灯,n^3更新即可。
具体的解释见:http://blog.csdn.net/PhilipsWeng/article/details/50767943~
告诉你一点人生的经验:inf要开大,0x7fffffff是不够的。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=1<<30;
int n,f[101][101][2],ans;
struct node{
int x,l;
bool operator < (const node&u)
{
return x<u.x;
}
}a[101];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=(node){read(),read()};
sort(a+1,a+n+1);a[0].x=-inf;
for(int i=0,las,next;i<=n;i++)
for(int j=0;j<=i;j++)
for(int k=0;k<2;k++)
{
ans=max(ans,f[i][j][k]);
las=a[j].x+k*a[j].l;
for(int z=i+1,mx=-inf,aa,bb;z<=n;z++)
for(int v=0;v<2;v++)
{
next=a[z].x+v*a[z].l;
if(next>mx) mx=next,aa=z,bb=v;
f[z][aa][bb]=max(f[z][aa][bb],f[i][j][k]+min(next-las,a[z].l)+mx-next);
}
}
printf("%d\n",ans);
return 0;
}