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

  • 1N105
  • 2C1014
  • 1x1<x2<<xN<C
  • 1vi109
  • All values in input are integers.

Subscores

  • 300 points will be awarded for passing the test set satisfying N100.

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

或者只有顺,或者只有逆

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+5;
 4 typedef long long ll;
 5 ll n,c,x[N],w[N],val[N],rval[N],shun[N],ni[N];
 6 int main()
 7 {
 8     cin>>n>>c;
 9     for(int i=1;i<=n;i++)
10     {
11         cin>>x[i]>>w[i];
12         val[i]=val[i-1]+w[i];//顺着到i的价值总和
13         shun[i]=max(shun[i-1],val[i]-x[i]);//顺着到i取最大
14     }
15     for(int i=n;i>=1;i--)
16     {
17         rval[i]=rval[i+1]+w[i];//逆着到i的价值总和
18         ni[i]=max(ni[i+1],rval[i]-c+x[i]);//逆着到i取最大
19     }
20     ll maxx=0;
21     for(int i=n;i>=1;i--)
22     {
23         maxx=max(maxx,rval[i]-2*(c-x[i])+shun[i-1]);//先逆着到i,再顺着到i-1
24         maxx=max(maxx,ni[i]);//只有逆
25     }
26     for(int i=1;i<=n;i++)
27     {
28         maxx=max(maxx,val[i]-2*x[i]+ni[i+1]);//先顺着到i,再逆着到i+1
29         maxx=max(maxx,shun[i]);//只有顺
30     }
31     cout<<maxx;
32     return 0;
33 }



猜你喜欢

转载自www.cnblogs.com/taozi1115402474/p/8906245.html