codeforces 559E Gerald and Path

160 篇文章 0 订阅
26 篇文章 0 订阅

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.

Input

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.

Output

Print a single integer — the maximum total length of the illuminated part of the path.

Examples
Input
3
1 1
2 2
3 3
Output
5
Input
4
1 2
3 3
4 3
6 2
Output
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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值