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. iOS设计标注处理方法

    如果设计只给3x的设计图 在做2x适配时有几种处理方法: 按逻辑像素,大小不变,比如3x手机上一张图的逻辑像素设为24x24point,那么2x手机上这张图的大小也设为24x24point,一般适用于 ...

  2. 分布式ID生成学习

    唯一 && 趋势有序 数据库auto_increment,多个写库时,每个写库不同的初始值和相同的步长(A(0,2)B(1,2)) 缺点:非绝对递增,写库压力大 DB只保存序列最大值, ...

  3. ACM__01背包,完全背包,多重背包

    今天写题的时候碰到了一道完全背包题,可是没有看出来,乱写了一通,浪费了一个晚上,顺便复习一下背包的知识 01背包 每种物品只能选择一次或者不选,求背包容量内的最大价值 先给出状态转移方程: f[i][ ...

  4. Maven仓库—Nexus环境搭建及使用

    使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包 http://blog.csdn.net/yanjun008/article/details/42084109 Nexus介 ...

  5. linux内核中的const成员是否可以修改?

    本文的基础知识:由于前半部分内容是转的,且不知道原文出处,没法给出原文地址,大家自行百度 const的实现机制 const究竟是如何实现的呢?对于声明为const的内置类型,例如int,short,l ...

  6. Android签名

    参考文档:http://blog.csdn.net/u010316858/article/details/53159678 http://www.cnblogs.com/wanqieddy/p/355 ...

  7. PowerDesigner 设置code不等于name

    设置 code不等于name:  工具 - 常规选项 - “Dialog” - "code不等于name",取消选中

  8. redis集群实战

    一.说明 redis 3.0集群功能出来已经有一段时间了,目前最新稳定版是3.0.5,我了解到已经有很多互联网公司在生产环境使用,比如唯品会.美团等等,刚好公司有个新项目,预估的量单机redis无法满 ...

  9. k近邻算法(KNN)

    k近邻算法(KNN) 定义:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别. from sklearn.model_selection ...

  10. hadoop2.7.7 测试安装 centos7

    useradd –m hadoop –s /bin/bash passwd hadoop   增加sudo权限 chmod u+w /etc/sudoers vi /etc/sudoers root ...