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. mingw 搭建Emscripten 环境

    mingw 环境的搭建可以参考网上很多文章,不复杂.但在搭建Emscripten 环境之前需要配置git 和python 和MSbuild.exe 还需要安装camke 默认安装之后应该是添加了 系统 ...

  2. vue:一个vue可以使用的视频插件

    网址:https://www.jianshu.com/p/e8e747e33ef0 1:安装依赖 npm install vue-video-player -S 2:引入配置(main.js) imp ...

  3. 6.5 Shell 算术计算

    6.5 Shell Arithmetic shell允许在其内计算表达式,可以通过以下方式使用:((中,let和带-i选项的declare命令中. 只能计算固定长度的整数,而且不会检查溢出,除0可以捕 ...

  4. WebStorm新创建项目介绍

    WebStorm创建一个项目 这里支持有很多的类型项目: Empty Project         ----一个空的项目 Html5 Boilerplate     ----HTML5开发框架 We ...

  5. 使用jQuery+huandlebars遍历展示对象中的数组

    兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...

  6. Android常见问题及解决方案收集

    1.手机安裝Apk时提示“无法打开文件” 出现这个问题,是因为下载的服务端对APK的MIME类型设置错误导致,一般会设置为application/vnd.android,其实这是错误的,应该设置为ap ...

  7. Mac中使用pycharm引包matplotlib失败

    最开始是使用matplotlib这个包,然后在pycharm中失败,然后在终端中pip install matplotlib,发现,安装了以后,pycharm依然找不到包. 代码如下: import ...

  8. DataTable--数据生成datatable

    将数据库查出的数据生成datatable 我们一般将数据库查询出的数据用实体接受在泛型集合,然后遍历集合,以将数据绑定到前台展示,在很多情况下,泛型集合不如datatable更方便将数据操作,这里简单 ...

  9. JAVA 课堂测试

    package ACC; /*信1705-2班 * 20173623 * 赵墨涵 */ public class Account { String accountID; String accountn ...

  10. linux下网络配置

    配置相关 http://bbs.acehat.com/thread-813-1-1.html