time limit per test : 2 seconds
memory limit per test : 256 megabytes
input : standard input
output : standard output
Xenia lives in a city that has n n n houses built along the main ringroad. The ringroad houses are numbered 1 1 1 through n n n in the clockwise order. The ringroad traffic is one way and also is clockwise.
Xenia has recently moved into the ringroad house number 1 1 1. As a result, she’s got m things to do. In order to complete the i i i-th task, she needs to be in the house number a i a_i ai and complete all tasks with numbers less than i i i. Initially, Xenia is in the house number 1 1 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.
Input
The first line contains two integers n n n and m m m ( 2 ≤ n ≤ 1 0 5 , 1 ≤ m ≤ 1 0 5 ) (2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5) (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m m m integers a 1 , a 2 , . . . , a m ( 1 ≤ a i ≤ n ) a_1, a_2, ..., a_m (1 ≤ a_i ≤ n) a1, a2, ..., am(1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.
Output
Print a single integer — the time Xenia needs to complete all tasks.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
input
4 3
3 2 3
output
6
input
4 3
2 3 3
output
2
Note
In the first test example the sequence of Xenia’s moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
题意
有 n n n个点围成的圆,每个点编号 1 1 1~ n n n,要求第 i i i个点的任务必须在 a i a_i ai点完成,并且必须按照顺序去完成任务。计算完成所有任务需要花费的最小时间(移动一个点花费时间为 1 1 1)
嘤嘤嘤,题意读了一年,真的是废了
Code
/*************************************************************************
> Author: WZY
> School: HPU
> Created Time: 2019-03-26 15:36:37
************************************************************************/
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstring>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <random>
#define ll long long
#define ull unsigned long long
#define lson o<<1
#define rson o<<1|1
#define ms(a,b) memset(a,b,sizeof(a))
#define SE(N) setprecision(N)
#define PSE(N) fixed<<setprecision(N)
#define bug cout<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
#define LEN(A) strlen(A)
const double E=exp(1);
const double eps=1e-9;
const double pi=acos(-1.0);
const int mod=1e9+7;
const int maxn=1e6+10;
const int maxm=1e3+10;
const int moha=19260817;
const int inf=1<<30;
const ll INF=1LL<<60;
using namespace std;
inline void Debug(){cerr<<'\n';}
inline void MIN(int &x,int y) {if(y<x) x=y;}
inline void MAX(int &x,int y) {if(y>x) x=y;}
inline void MIN(ll &x,ll y) {if(y<x) x=y;}
inline void MAX(ll &x,ll y) {if(y>x) x=y;}
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
cerr<<arg<<"";Debug(rest...);}
int a[maxn];
int vis[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);cin.tie(0);
cout.precision(20);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i];
ll ans=0;
int res=1;
vis[res]=1;
ans+=a[1]-1;
while(res<m)
{
if(a[res]>a[res+1])
ans+=(n-a[res])+a[res+1];
else if(a[res]==a[res+1])
{
res++;
continue;
}
else
ans+=(a[res+1]-a[res]);
res++;
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
#endif
return 0;
}