• Static Sushi AtCoder


    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 Npieces 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 vikilocalories.

    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 1kilocalories 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 120kilocalories. 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 192kilocalories 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.

    思路:

    所有的路线等效后只有4种类型。

    1,只顺时针走。

    2,只逆时针走。

    3,先顺时针,再逆时针。

    4,先逆时针,在瞬时针。

    那么我们不妨处理对于每一个寿司,顺时针和逆时针到这个寿司最大的收益(可能没有走到这个节点,在前面节点的时候就走掉了。)

    然后我们枚举对于每一个寿司  i ,

    顺时针走到这个寿司的最大收获

    逆时针走到这个寿司的最大收获

    顺时针走到这个寿司,在逆时针走到 i -1 寿司的最大收获

    逆时针走到这个寿司,在顺时针走到 i + 1 寿司的最大收获

    然后取这n个中的最大值,即是答案。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    ll n;
    ll c;
    struct node
    {
        ll v;
        ll x;
    };
    node a[maxn];
    ll shun[maxn];
    ll ni[maxn];
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
        //freopen("D:\common_text\code_stream\out.txt","w",stdout);
        gbtb;
        cin>>n>>c;
        repd(i,1,n)
        {
            cin>>a[i].x>>a[i].v;
        }
        ll ans=0ll;
        repd(i,1,n)
        {
            shun[i]=shun[i-1]+a[i].v-(a[i].x-a[i-1].x);
        }
        repd(i,1,n)
        {
            shun[i]=max(shun[i-1],shun[i]);
        }
        a[n+1].x=c;
        for(int i=n;i>=1;i--)
        {
            ni[i]=ni[i+1]+a[i].v-(a[i+1].x-a[i].x);
        }
        for(int i=n;i>=1;i--)
        {
            ni[i]=max(ni[i],ni[i+1]);
        }
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,shun[i]);
            ans=max(ans,ni[i]);
            ans=max(ans,shun[i]-a[i].x+ni[i+1]);
            ans=max(ans,ni[i]-(c-a[i].x)+shun[i-1]);
        }    
        cout<<ans<<endl;
        
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    常用的清理 Kubernetes 集群命令
    mask彻底禁用systemctl服务
    ansibleplaybook指定role limit
    极速理解设计模式系列:16.迭代器模式(Iterator Pattern)
    极速理解设计模式系列:19.备忘录模式(Memento Pattern)
    极速理解设计模式系列:8.策略模式(Strategy Pattern)
    极速理解设计模式系列:6.适配器模式(Adapter Pattern)
    PostSharp AOP编程:2.PostSharp的OnMethodBoundaryAspect类基本组成
    极速理解设计模式系列:18.访问者模式(Visitor Pattern)
    极速理解设计模式系列:10.抽象工厂模式(Abstract Factory Pattern)
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10707433.html
Copyright © 2020-2023  润新知