Nowcoder deco的abs

Original title link

Title Description

deco recently fell in love abs, recalled in a dream that one day he played a title own abs seen before:
Given a series of length n, the i-th digit is aia_iai, for each number, you can choose to give him any time plus a number d, such that Σi = 2n|ai-ai-1| \ sum \ limits_ {i = 2} ^ n | a_i-a_ {i-1} | i = 2Σn | the minimum value | ai -ai-1

But deco was very naive that he did not do it, and now he lay in bed playing mobile phone do not want to take up the computer, you can help him out?

Enter a description:

The first line, two numbers, n, D
a second row, the number n, representative of the i-th aia_iai

Output Description:

Line, a number representing Σi = 2n|ai-ai-1| \ sum \ limits_ {i = 2} ^ n | a_i-a_ {i-1} | i = 2Σn |ai -ai- 1 | minimum

Example 1

Entry

4 3
1 7 8 1

Export

2

Remarks:

For 30% 30% 30% of the data, n≤105n \ leq 10 ^ 5n≤105
data for 50% 50% 50%, n × d≤108n \ times d \ leq 10 ^ 8n × d≤108
to 100% 100% 100% data, 2≤n≤1072 \ leq n \ leq 10 ^ 72≤n≤107,1≤d≤100001 \ leq d \ leq 100001≤d≤10000,1≤ai≤1061 \ leq a_i \ leq 10 ^ 61≤ai ≤106

#include<cstdio>
#define int long long
using namespace std;
typedef long long ll;

inline void input(ll &x){
    ll ans=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        ans=ans*10+c-48;
        c=getchar();
    }
    x=ans*f;
}

inline void output(ll x){
    if(x<0)x=-x,putchar('-');
    if(x>9)output(x/10);
    putchar(x%10+48);
}

inline void writeln(int x){
    output(x);
    putchar('\n');
}

inline int abs(int x){
    return x>0?x:-x;
}

inline int min(int a,int b){
    return a<b?a:b;
}

int n,d,x,last,ans;

signed main(){
    input(n);input(d);
    for(int i=1;i<=n;i++){
        input(x);
        x%=d;
        if(i!=1)ans+=min(abs(x-last),d-abs(x-last));
        last=x;
    }
    writeln(ans);
}

Note that in both cases contained min

So far I have not figure out why not add code read optimization bizarre timeout

And why my code is not open longlong bizarre WA

Well I could not RP (

Guess you like

Origin www.cnblogs.com/Y15BeTa/p/11404519.html