Title Description
Small $ s $ loved origami.
One day, he got a long tape, which he left to right $ N $ evenly divided into unit lengths, and are marked with numbers $ 0 \ sim n $ at the boundaries of each.
Then start small $ s $ boring origami, every time he would choose a number, position along the tape folded this figure is currently located (if it has the equivalent of doing nothing on the boundary).
After a small $ s $ $ M $ wants to know how much longer times folded paper tape.
Input Format
The first line contains two positive integers $ N $ and $ M $, the length of the tape and the number of representations of operations.
The next line contains the integers $ M $ $ D_i $, where $ D_i $ $ I $ represents a number of times selected.
Output Format
Only one digital output file, that is, the final length of the tape.
Sample
Sample input:
5 2
3 5
Sample output:
2
Data range and tips
Data $ 60 \% of $ $ N \ leqslant 3,000, M \ leqslant 3,000 $.
Data $ 100 \% of $ $ N \ leqslant {10} ^ {18}, M \ leqslant 3,000 $.
answer
$ 60 \% $ algorithm:
Direct Simulation folding process, record the location of each point can now, every update violence.
Remember initialization.
Time complexity: $ \ Theta (N \ times M) $.
Expectations score: $ 60 $ points.
Actual score: $ 60 $ points.
$ 100 \% $ algorithm:
We do not find the location of each tape with care, just like with consideration of the position of the current operation.
Flanging performed after each $ \ Theta (M) $ reference to the weight of all the operations, as the expression $ D_j = abs (D_i-D_j) $.
Time complexity: $ \ Theta (m ^ 2) $.
Expectations score: $ 100 $ points.
Actual score: $ 100 $ points.
Code time
#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long l,r;
long long wzc[5000];
int main()
{
scanf("%lld%lld",&n,&m);
l=0,r=n;
for(int i=1;i<=m;i++)
{
scanf("%lld",&wzc[i]);
for(int j=1;j<i;j++)
if(wzc[j]>=wzc[i])
wzc[i]=2LL*wzc[j]-wzc[i];
r=max(r,2LL*wzc[i]-l);
l=wzc[i];
}
printf("%lld",r-l);
return 0;
}
rp ++