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.

meaning of the title

n lines, each line x[i] and v[i] represent the position of the sushi and the energy obtained after eating

There is a circular counter in the restaurant with a perimeter of C. Nakahashi starts from 1 and consumes 1k calories for every 1m he walks. Nakahashi can leave the restaurant at any time and ask for the maximum energy that Nakahashi can take away.

answer

Considering n<=1e5, shun[i] goes to i along the way, and ni[i] goes to i in the opposite direction

There is a double cycle of forward and reverse i, j is judged to follow to i, and reverse to the maximum of j

Or only the straight line, or only the reverse, the maximum of the three cases can only get 300 points

How to optimize the complexity?

We can think about it in this way, shun[i] goes to the maximum of i along the way, and ni[i] goes to the maximum of i in the opposite direction

If there is an ups and downs, consider going down to i first, and then down to i+1

Then consider walking backwards to i, and walking to i-1

Either only the straight, or only the inverse

code

 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];//Follow the value of i to the sum of 
13          shun[i]=max(shun[i- 1 ],val[i]-x[i]); // Follow to i to take the maximum 
14      }
 15      for ( int i=n ;i>= 1 ;i-- )
 16      {
 17          rval[i]=rval[i+ 1 ]+w[i]; // reverse to the sum of the value of i 
18          ni[i]=max(ni[i+ 1 ],rval[i]-c+x[i]); // Reverse to i and take the maximum 
19      }
 20      ll maxx= 0 ;
 21      for ( int i=n;i>= 1 ;i-- )
 22     {
 23          maxx=max(maxx,rval[i]- 2 *(cx[i])+shun[i- 1 ]); // First go back to i, then go to i-1 
24          maxx=max( maxx,ni[i]); // only inverse 
25      }
 26      for ( int i= 1 ;i<=n;i++ )
 27      {
 28          maxx=max(maxx,val[i]- 2 *x[i]+ ni[i+ 1 ]); // Follow to i first, then reverse to i+1 
29          maxx=max(maxx,shun[i]); // Only follow 
30      }
 31      cout<< maxx;
 32     return 0;
33 }

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650526&siteId=291194637