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.求最少需要几 ...
随机推荐
- Spark/Storm/Flink
https://www.cnblogs.com/yaohaitao/p/5703288.html Spark Streaming与Storm的应用场景 对于Storm来说:1.建议在那种需要纯实时, ...
- C# 中奇妙的函数–String Split 和 Join
很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能需要加入分隔符(如CSV文件中的逗号),或使用一个分隔符来合并字符串序列. 很多人都知道使用split()的方法,但使用与其对应的Join ...
- Spark 调优(转)
Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...
- Servlet基本_初期化パラメータ、外部ファイル
1.サーブレットの初期化パラメータサーブレットの初期化パラメータを利用するには.必ずweb.xmlにおいてサーブレットマッピングを指定する必要がある.(Tomactのinvokerサーブレットは利用で ...
- 虚拟机centos NAT模式 配置静态ip
主要的设置有1.配置ip地址段,2.配置NAT(网关.ip地址端.子网掩码),3.修改网卡配置文件(/etc/sysconfig/network-scripts/ifcfg-eth0 ),4.重启网卡 ...
- VC中BSTR、Char和CString类型的转换
1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...
- HTML实现页面自动跳转的五种方法
下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件. 1)html的实现 复制代码 代码如下: <head> ...
- Matlab实现BP网络识别字母
训练样本空间 每个样本使用5×5的二值矩阵表征一个字母.一共10个字母类型,分别是N,I,L,H,T,C,E,F,Z,V.每个字母9个样本.共90个. N1=[1,0,0,0,1; 1,0,0,0 ...
- Java解决小孩围圈问题
问题描述:一堆小孩围成一个圈,从第一个小孩开始数,每数到第三个便把第三个孩子删去,数到只剩一个小孩为止,并求该孩子的具体编号. 解决办法 1. package test; public class C ...
- windows 下 redis安装
在D盘新建文件夹[redis],右键解压Redis ZIP包,把所有文件解压到redis文件夹中.(其他盘符也可以滴^_^) 文件介绍: redis-benchmark.exe #基准 ...