Codeforces 339B: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 nnn houses built along the main ringroad. The ringroad houses are numbered 111 through nnn in the clockwise order. The ringroad traffic is one way and also is clockwise.

Xenia has recently moved into the ringroad house number 111. As a result, she’s got m things to do. In order to complete the iii-th task, she needs to be in the house number aia_iai and complete all tasks with numbers less than iii. Initially, Xenia is in the house number 111, 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 nnn and mmm (2 ≤ n ≤ 105, 1 ≤ m ≤ 105)(2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5)(2n105,1m105). The second line contains mmm integers a1, a2, ..., am(1 ≤ ai ≤ n)a_1, a_2, ..., a_m (1 ≤ a_i ≤ n)a1,a2,...,am(1ain). 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.

题意

nnn个点围成的圆,每个点编号111~nnn,要求第iii个点的任务必须在aia_iai点完成,并且必须按照顺序去完成任务。计算完成所有任务需要花费的最小时间(移动一个点花费时间为111

嘤嘤嘤,题意读了一年,真的是废了

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;
}

转载于:https://www.cnblogs.com/Friends-A/p/11054962.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值