• AtCoder Regular Contest 077 E


    E - guruguru


    Time limit : 2sec / Memory limit : 256MB

    Score : 700 points

    Problem Statement

    Snuke is buying a lamp. The light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.

    The first button is a "forward" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.

    The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.

    Snuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness. He is planning to change the brightness n−1 times. In the i-th change, the brightness level is changed from ai to ai+1. The initial brightness level is a1. Find the number of times Snuke needs to press the buttons when x is set to minimize this number.

    Constraints

    • 2≤n,m≤105
    • 1≤aim
    • aiai+1
    • nm and ai are integers.

    Input

    Input is given from Standard Input in the following format:

    n m
    a1 a2an
    

    Output

    Print the minimum number of times Snuke needs to press the buttons.


    Sample Input 1

    4 6
    1 5 1 4
    

    Sample Output 1

    5
    

    When the favorite brightness level is set to 12345 and 6, Snuke needs to press the buttons 89756 and 9 times, respectively. Thus, Snuke should set the favorite brightness level to 4. In this case, the brightness is adjusted as follows:

    • In the first change, press the favorite button once, then press the forward button once.
    • In the second change, press the forward button twice.
    • In the third change, press the favorite button once.

    Sample Input 2

    10 10
    10 9 8 7 6 5 4 3 2 1
    

    Sample Output 2

    45
    

     举个例子对于从2->8,原本花费为6,

    如果将快捷键设置为4,那么将省去1次的花费,

    如果将快捷键设置为5,那么将省去2次的花费,

    如果将快捷键设置为6,那么将省去3次的花费,

    如果将快捷键设置为7,那么将省去4次的花费,

    如果将快捷键设置为8,那么将省去5次的花费。可以视为4->8的一个起始为1,公差为1的等差数列

    对于所有x->y,维护线段树,每个节点维护该区间的起始值和公差。

    所有维护结束后,再向下更新到叶子节点,就可以知道每个节点可以省去多少次花费,找到最大值就可以了。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define pb push_back
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)>(y)?(y):(x))
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1e9+10;
    const ll llinf=1e16+10;
    const int maxn=1e5+10;
    const int maxm=1e5+10;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    int n,m;
    struct node
    {
        ll st,d;
        node(){st=d=0;}
    }tree[maxm*4];
    int op[maxn];
    ll ans;
    void add(int left,int right,ll x,int l,int r,int rt)
    {
        if(left==l&&right==r)
        {
            tree[rt].st+=x;
            tree[rt].d++;
            return ;
        }
        if(left>right)
        {
            add(left,m,x,l,r,rt);
            add(1,right,m-left+1+x,l,r,rt);
        }
        else
        {
            int mid=(l+r)/2;
            if(left>=mid+1) add(left,right,x,mid+1,r,rt*2+1);
            else if(right<=mid) add(left,right,x,l,mid,rt*2);
            else
            {
                add(left,mid,x,l,mid,rt*2);
                add(mid+1,right,x+mid+1-left,mid+1,r,rt*2+1);
            }
        }
    }
    void refresh(int l,int r,int rt)
    {
        if(l==r)
        {
            ans=max(ans,tree[rt].st);
            return ;
        }
        int mid=(l+r)/2;
        tree[rt*2].st+=tree[rt].st;
        tree[rt*2].d+=tree[rt].d;
        refresh(l,mid,rt*2);
        tree[rt*2+1].st+=tree[rt].st+tree[rt].d*(mid+1-l);
        tree[rt*2+1].d+=tree[rt].d;
        refresh(mid+1,r,rt*2+1);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d %d",&n,&m))
        {
            ans=0;
            for(int i=1;i<=n;i++)
                scanf("%d",&op[i]);
            for(int i=1;i<n;i++)
            {
                if((op[i]+1)%m==(op[i+1])%m)
                  continue;
                if(op[i]+2>m)
                    add((op[i]+2)%m,op[i+1],1,1,m,1);
                else add(op[i]+2,op[i+1],1,1,m,1);
            }
            refresh(1,m,1);
            ll sum=0;
            for(int i=1;i<n;i++)
            {
                sum+=((op[i+1]-op[i])%m+m)%m;
            }
            printf("%lld
    ",sum-ans);
        }
        return 0;
    }
  • 相关阅读:
    Django之数据库表的创建和ORM相关操作
    Django后续和Ajax初识
    阿里云Maven中央仓库配置
    java/javascript 时间操作工具类
    原生javascript实现文件异步上传
    MySQL中的存储函数和存储过程的简单示例
    java同步锁的正确使用
    浅谈javascript的面向对象思想
    java与javascript对cookie操作的工具类
    json字符串与json对象的相互转换
  • 原文地址:https://www.cnblogs.com/mgz-/p/7115489.html
Copyright © 2020-2023  润新知