CodeForces-339B-Xenia and Ringroad (循环队列,水题)
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 a i 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 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.
思路:
路径移动类似循环队列移动,注意点:使用long long 保证不溢出
#include<bits/stdc++.h>
using namespace std;
long long m, n, x = 1, s, y;
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
for (cin >> n >> m; m--; s += (n + y - x) % n, x = y)//x起点位置,y目标位置
cin >> y;
cout << s;
}
CodeForces-339B-Xenia and Ringroad (循环队列,水题)的更多相关文章
- CodeForces 339B Xenia and Ringroad(水题模拟)
题意:给定 n 个地方,然后再给 m 个任务,每个任务必须在规定的地方完成,并且必须按顺序完成,问你最少时间. 析:没什么可说的,就是模拟,记录当前的位置,然后去找和下一个位置相差多长时间,然后更新当 ...
- codeforces Gym 100187L L. Ministry of Truth 水题
L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- Codeforces Round #185 (Div. 2) B. Archer 水题
B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...
- Educational Codeforces Round 14 A. Fashion in Berland 水题
A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...
- Codeforces Round #360 (Div. 2) A. Opponents 水题
A. Opponents 题目连接: http://www.codeforces.com/contest/688/problem/A Description Arya has n opponents ...
- Codeforces Round #190 (Div. 2) 水果俩水题
后天考试,今天做题,我真佩服自己... 这次又只A俩水题... orz各路神犇... 话说这次模拟题挺多... 半个多小时把前面俩水题做完,然后卡C,和往常一样,题目看懂做不出来... A: 算是模拟 ...
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告
对于这道水题本人觉得应该应用贪心算法来解这道题: 下面就贴出本人的代码吧: #include<cstdio> #include<iostream> using namespac ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- Codeforces 777D:Cloud of Hashtags(水题)
http://codeforces.com/problemset/problem/777/D 题意:给出n道字符串,删除最少的字符使得s[i] <= s[i+1]. 思路:感觉比C水好多啊,大概 ...
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...
随机推荐
- redis基础命令复习(Sring,Hash,List,Set,SortedSet)
1,Redis数据结构: https://redis.io/commands 2,Redis命令---Redis通用命令(常见的有,keys,del,exists,expire,ttl) 2.1,ke ...
- Java八股面试整理(3)
21.说一说hashCode()和equals()的关系 hashCode()用于获取哈希码(散列码),eauqls()用于比较两个对象是否相等,它们应遵守如下规定: 如果两个对象相等,则它们必须有相 ...
- 掌握这些,轻松管理BusyBox:inittab文件的配置和作用解析
BusyBox 是一个轻量级的开源工具箱,其中包含了许多标准的 Unix 工具,例如 sh.ls.cp.sed.awk.grep 等,同时它也支持大多数关键的系统功能,例如自启动.进程管理.启动脚本等 ...
- controller加载控制与业务bean加载控制
1.因功能的不同,如何避免Spring错误加载到SpringMVC的bean--加载Spring控制的bean的时候排除掉SpringMVC控制的bean. package com.itheima.c ...
- 如何为项目配置opencv
如何为项目配置opencv 13/100 发布文章 public669 未选择任何文件 new 配置: 包含目录: D:\OpenCV\opencv\build\include D:\OpenCV\o ...
- CompletableFuture异步编程
1.创建 /** * public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier ...
- const和C指针
转载:https://mp.weixin.qq.com/s?__biz=Mzk0NDYzNTI1Ng==&mid=2247483733&idx=1&sn=9af9bc64a0b ...
- 2023“强网杯”部分WP
强网先锋 SpeedUp 题目 我的解答: 分析代码可知是求2的27次方的阶乘的每一位的和. 使用在线网址直接查看:https://oeis.org/A244060/list 然后sha256加密 f ...
- 华为云Astro的前世今生:用7年时间革新低代码开发观念
摘要:深扒华为云Astro低代码平台的前世今生,其成功之路显然是一条"个性"之路. 本文分享自华为云社区<华为云Astro的前世今生:用7年时间革新低代码开发观念>,作 ...
- 云图说|ModelArts Pro,为企业级AI应用打造的专业开发套件
摘要: ModelArts Pro 为企业级AI应用打造专业开发套件.基于华为云的先进算法和快速训练能力,提供预置工作流和模型,提升企业AI应用的开发效率,降低开发难度. AI技术的高门槛与落地难是中 ...