题目衔接:http://codeforces.com/problemset/problem/339/B
Xenia and Ringroad
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 houses built along the main ringroad. The ringroad houses are numbered 1 through 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. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 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 and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers 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 %I64dspecifier.
Examples
input
Copy
4 3 3 2 3output
Copy
6input
Copy
4 3 2 3 3output
Copy
2Note
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个点的道路,现在给你在第i个位置上的任务m个,问你完成所有任务需要的时间,注意完成第i个任务前必须先完成前i-1个任务。
思路:模拟一下过程吧,这里我先对每个任务先处理下,然后比较这个任务点和上一个任务点,如果大于直接过来就可以,如果小于,那么还需要转一圈回来,然后计算整个过程的时间即可
代码:
/*
题目大意:有n个地方组成一个环,现在有m件事需要完成
并且完成这件事之前必须完成前面的事,问你完成所有的任务需要多少时间
思路:
*/
#include<map>
#include<set>
#include <vector>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
const int maxn=2e5+10;
const double pi=acos(-1.0);
const int N=201;
const int mod=1e9+7;
ll a[maxn];
int main()
{
ll n,m;
ll ans=0;
cin>>n>>m;
for(int i=0; i<m; i++)
{
cin>>a[i];
a[i]--;
}
ll last=0;
for(int i=0; i<m; i++)
{
if(a[i]>=last)
{
ans+=(a[i]-last);
}
else
ans+=(n-(last-a[i]));
last=a[i];
}
cout<<ans<<endl;
return 0;
}