AtCoder Regular Contest 096 - D Static Sushi

20 篇文章 0 订阅

D - Static Sushi


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is xi meters. Also, the i-th sushi has a nutritive value of vi kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

  • 1N105
  • 2C1014
  • 1x1<x2<<xN<C
  • 1vi109
  • All values in input are integers.

Subscores

  • 300 points will be awarded for passing the test set satisfying N100.

Input

Input is given from Standard Input in the following format:

N C
x1 v1
x2 v2
:
xN vN

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.




题意:

在一张周长为C的大圆桌上,边缘上摆着n个寿司,他有vi卡路里,坐标为(0-C),不包括。

注意圆桌的坐标的0和C是重合的。你从0出发,任意走,走1米消耗1卡路里,吃到寿司可以增加vi卡路里。你可以在任意时刻离开,

问你得到最大的卡路里是多少?


POINT:

1.拿的寿司肯定是连续的。

2.只能回头一次。

所以,从起点顺时针记录a[i],从0顺时针走到i寿司可以得到的卡路里。

从起点逆时针记录b[i],从0逆时针走到i寿司可以得到的卡路里。

然后更新为max类型数组。a[i]代表顺时针走到i寿司可以得到最大的卡路里(即包括了[1,i-1]所有情况的最优)。

b[i]类似。

然后遍历a[i],加上对应的b[i],减去重复走了一次的路长,就是答案。

注意不要漏掉不折回的情况。


#include<stdio.h>
#include<math.h>
#include <iostream>
using namespace std;
#define  LL long long

const int maxn = 1e5+55;

LL a[maxn],b[maxn];
LL x[maxn],v[maxn];
LL sum[maxn];
int main()
{
	LL n,c;
	cin>>n>>c;
	for(int i=1;i<=n;i++)
		cin>>x[i]>>v[i];

	for(LL i=1;i<=n;i++)
		a[i]=a[i-1]+v[i]-(x[i]-x[i-1]);
	x[n+1]=c;
	for(LL i=n;i>=1;i--)
		b[i]=b[i+1]+v[i]-(x[i+1]-x[i]);

	for(LL i=2;i<=n;i++)
		a[i]=max(a[i-1],a[i]);

	for(LL i=n-1;i>=1;i--)
		b[i]=max(b[i],b[i+1]);
	LL ans=0;
	for(LL i=1;i<=n;i++){
		ans=max(ans,-x[i]+a[i]+b[i+1]); //顺时针,再折回
		ans=max(ans,-(c-x[i])+b[i]+a[i-1]);//逆时针 再折回
		ans=max(ans,a[i]);//顺时针,不折回
		ans=max(ans,b[i]);//逆时针,不折回
	}
	cout<<ans<<endl;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值