AtCoder Regular Contest 096 D - Static Sushi(线性dp)
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
3 20
2 80
9 120
16 1
Sample Output 1
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
3 20
2 80
9 1
16 120
Sample Output 2
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
1 100000000000000
50000000000000 1
Sample Output 3
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
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
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)的更多相关文章
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
- [AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)
题目链接:https://arc096.contest.atcoder.jp/tasks/arc096_c Time limit : 4sec / Memory limit : 512MB Score ...
- Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)
洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...
- Atcoder Regular Contest 096 C - Everything on It(组合数学)
Atcoder 题面传送门 & 洛谷题面传送门 简单题,由于这场 arc 的 F 是 jxd 作业而我不会做,所以只好来把这场的 E 水掉了. 我们记 \(f(i)\) 为钦定 \(i\) 个 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
随机推荐
- linux 源码编译php的参数
./configure --prefix=/usr/local/php-5.3.5 --with-config-file-path=/usr/local/php-5.3.5/etc --with-co ...
- javascript:图片转base64
第一种: <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta ...
- U盘无法访问
U盘无法访问 方法/步骤 首先,Win+R,打开“运行”窗口. 在打开的运行窗口中,输入cmd回车 这时会打开这样的一个窗口 这时输入chkdsk g: /f 需要说明的是,g这个 ...
- A Swifr Tour
Tradition suggests that the first program in a new language should print the words "Hello ,worl ...
- KJMusic完整音乐项目
KJMusic完整音乐项目 KJMusic是一个完整音乐项目,这个项目从欢迎页面到首页以及音乐播放页面都做得非常不错.并且本音乐支持本地音乐,和音乐电台,支持切换上下首个.本项目还支持侧滑出现menu ...
- 吴裕雄 python深度学习与实践(6)
from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath ...
- gparted增加Ubuntu14.04根目录空间(转)
转自:https://blog.csdn.net/t765833631/article/details/79031063 在win7上装了Ubuntu14.04双系统后,突然发现ubuntu开机会弹出 ...
- 微信小程序生命周期——小程序的生命周期及页面的生命周期。
最近在做微信小程序开发,也发现一些坑,分享一下自己踩过的坑. 生命周期是指一个小程序从创建到销毁的一系列过程. 在小程序中 ,通过App()来注册一个小程序 ,通过Page()来注册一个页面. 首先来 ...
- k8s operator
https://coreos.com/blog/introducing-operators.html Site Reliability Engineer(SRE)是通过编写软件来运行应用程序的人员. ...
- HTTP是用来做什么的
(一)HTTP协议介绍 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目 ...