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

  • 1≤N≤105
  • 2≤C≤1014
  • 1≤x1<x2<…<xN<C
  • 1≤vi≤109
  • All values in input are integers.

Subscores

  • 300 points will be awarded for passing the test set satisfying N≤100.

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.


Sample Input 1

Copy
3 20
2 80
9 120
16 1

Sample Output 1

Copy
191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.


Sample Input 2

Copy
3 20
2 80
9 1
16 120

Sample Output 2

Copy
192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.


Sample Input 3

Copy
1 100000000000000
50000000000000 1

Sample Output 3

Copy
0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.


Sample Input 4

Copy
15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

Copy
6500000000

All these sample inputs above are included in the test set for the partial score.

题意

n行,每行x[i]和v[i]代表寿司所在的位置和吃完后获得的能量

餐厅里有个圆形柜台周长为C,Nakahashi从1开始,每走1m消耗1k卡能量,Nakahashi可以在任何时候离开餐馆,求Nakahashi所能带走的最大能量

题解

考虑到n<=1e5,shun[i]顺着走到i,ni[i]逆着走到i

有顺又有逆二重循环i,j判断顺着到i,逆着到j最大

或者只有顺,或者只有逆,3种情况取最大只能拿300分

如何优化掉复杂度呢?

我们可以这样考虑,shun[i]顺着走到i的最大,ni[i]逆着走到i的最大

有顺有逆的话,考虑先顺着走到i,再逆着走到i+1

然后考虑逆着走到i,顺着走到i-1

或者只有顺,或者只有逆

代码

 #include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
typedef long long ll;
ll n,c,x[N],w[N],val[N],rval[N],shun[N],ni[N];
int main()
{
cin>>n>>c;
for(int i=;i<=n;i++)
{
cin>>x[i]>>w[i];
val[i]=val[i-]+w[i];//顺着到i的价值总和
shun[i]=max(shun[i-],val[i]-x[i]);//顺着到i取最大
}
for(int i=n;i>=;i--)
{
rval[i]=rval[i+]+w[i];//逆着到i的价值总和
ni[i]=max(ni[i+],rval[i]-c+x[i]);//逆着到i取最大
}
ll maxx=;
for(int i=n;i>=;i--)
{
maxx=max(maxx,rval[i]-*(c-x[i])+shun[i-]);//先逆着到i,再顺着到i-1
maxx=max(maxx,ni[i]);//只有逆
}
for(int i=;i<=n;i++)
{
maxx=max(maxx,val[i]-*x[i]+ni[i+]);//先顺着到i,再逆着到i+1
maxx=max(maxx,shun[i]);//只有顺
}
cout<<maxx;
return ;
}

AtCoder Regular Contest 096 D - Static Sushi(线性dp)的更多相关文章

  1. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  2. [AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)

    题目链接:https://arc096.contest.atcoder.jp/tasks/arc096_c Time limit : 4sec / Memory limit : 512MB Score ...

  3. Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)

    洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...

  4. Atcoder Regular Contest 096 C - Everything on It(组合数学)

    Atcoder 题面传送门 & 洛谷题面传送门 简单题,由于这场 arc 的 F 是 jxd 作业而我不会做,所以只好来把这场的 E 水掉了. 我们记 \(f(i)\) 为钦定 \(i\) 个 ...

  5. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  6. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  7. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  8. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  9. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

随机推荐

  1. 使用Prometheus+Grafana监控MySQL实践

    一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...

  2. ABAP-权限查询-用户信息系统

    事务代码:SUIM

  3. English Conversation – NUMBERS

    English Conversation – NUMBERS Share Tweet Share Tagged With: Numbers Study the pronunciation of num ...

  4. linux目录结构详解(以suse linux 10为例)

    一.文件系统结构 位于Linux系统的最顶端即根目录是/.Linux的文件系统的入口就是/,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. 它之下的子目录有: ...

  5. C++ 使用VS2010创建MFC ActiveX工程项目

    1.ActiveX的基本概念 ActiveX控件可以看作是一个极小的服务器应用程序,它不能独立运行,必须嵌入到某个容器程序中,与该容器一起运行.这个容器包括WEB网页,应用程序窗体等... Activ ...

  6. 吴裕雄 25-MySQL 临时表

    MySQL 临时表MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间.临时表在MySQL 3.23版本中添加,如果你 ...

  7. 吴裕雄 24-MySQL 索引

    MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度.打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一 ...

  8. 局部变量and全局变量

    局部变量 <1>什么是局部变量 如下图所示: <2>小总结 局部变量,就是在函数内部定义的变量 不同的函数,可以定义相同的名字的局部变量,但是各用个的不会产生影响 局部变量的作 ...

  9. tensorflow的save和restore

    使用tensorflow中的save和restore可以对模型进行保存和恢复 import tensorflow as tf v1 = tf.Variable(tf.random_normal([1, ...

  10. linux安装memcached和php的memcache扩展 (已使用)

    所需软件libevent-1.4.6-stable.tar.gz (http://monkey.org/~provos/libevent/)memcache-2.2.3.tgz (http://pec ...