Codeforces 339B:Xenia and Ringroad(水题)
发布日期:2022-04-01 13:25:18 浏览次数:39 分类:博客文章

本文共 2961 字,大约阅读时间需要 9 分钟。

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
houses built along the main ringroad. The ringroad houses are numbered
1
1
through
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
. As a result, she’s got m things to do. In order to complete the
i
i
-th task, she needs to be in the house number
a
i
a_i
and complete all tasks with numbers less than
i
i
. Initially, Xenia is in the house number
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
and
m
m
(
2
n
1
0
5
,
1
m
1
0
5
)
(2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5)
. The second line contains
m
m
integers
a
1
,
a
2
,
.
.
.
,
a
m
(
1
a
i
n
)
a_1, a_2, ..., a_m (1 ≤ a_i ≤ 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
个点围成的圆,每个点编号
1
1
~
n
n
,要求第
i
i
个点的任务必须在
a
i
a_i
点完成,并且必须按照顺序去完成任务。计算完成所有任务需要花费的最小时间(移动一个点花费时间为
1
1

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

Code

/*************************************************************************	 > Author: WZY	 > School: HPU	 > Created Time:   2019-03-26 15:36:37	 ************************************************************************/#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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<
x) x=y;}inline void MIN(ll &x,ll y) {if(y
x) x=y;}template
void Debug(FIRST arg, REST... rest){ cerr<
<<"";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
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<
<

转载地址:https://www.cnblogs.com/Friends-A/p/11054962.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:第九届河南理工大学算法程序设计大赛 正式赛(部分题解)
下一篇:Codeforces1132A——Regular Bracket Sequence(水题)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月03日 22时18分54秒