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 ...
随机推荐
- day2-JS基础&流程控制
typora-root-url: img 自增自减运算符 1.基本使用 内置提供 ++.--运算符 是用于将变量本身进行加1或者减1操作 // 1.基本使用 var i = 10; i++;//等价于 ...
- Vue源码学习(十七):实现computed计算属性
好家伙,本章我们尝试实现computed属性 0.完整代码已开源 https://github.com/Fattiger4399/analytic-vue.git 1.分析 1.1computed的常 ...
- crm系统功能有哪些?
CRM系统是指客户关系管理系统,其核心理念是将客户视为企业的宝贵资源,为了最大化地发掘和利用客户价值,运用各种先进的技术与方法来促进商业活动中所有涉及到客户的流程和业务,所以实际上CRM系统也是一个综 ...
- teleport 服务端配置文件说明
teleport 服务端配置文件说明 teleport配置文件位于/usr/local/teleport/data/etc目录下.服务器端包含两个配置文件: core.ini 和 web.ini,其中 ...
- 实用指南:打造卓越企业BI实施解决方案
前言 随着大数据时代的到来,商业智能(BI)工具变得非常重要.一个全面的商业智能方案可以支持数据驱动的决策并提高决策效率,同时还可以准确反映企业运行状态,为企业持续增长提供新的动力.本文小编将为大家介 ...
- lottie 动画在 vue 中的使用
前言 最近我所负责的项目中,我采用了动画效果,并开始使用 gif 来实现.然而,在实践过程中,我发现 gif 格式的动画在 git 中出现了明显的锯齿感,这让我非常困扰.为了追求更完美的表现效果,我最 ...
- python tkinter 使用(八)
python tkinter 使用(八) 本文主要讲下tkinter库中的其他的一些模块,如simpleDialog,scrollerText等. 1: simpleDialog simpledial ...
- Android SDK Manager 报错“加载 SDK 组件信息失败”。(Android SDK Manager complains with "Loading SDK component information failed."
[解决方案]: 将存储库设置更改为 Google .因此,在 android SDK 管理器上点击齿轮图标(设置),然后点击 Repository -> Google.
- Codeforce Round 916(div3)
Codeforces Round 916(div3) [Problem - A - Codeforces]:Problemsolving Log A.题 直接看样例进行分析,发现每一次出现的字符代表着 ...
- 神经网络优化篇:详解神经网络的权重初始化(Weight Initialization for Deep NetworksVanishing / Exploding gradients)
神经网络的权重初始化 这是一个神经单元初始化地例子,然后再演变到整个深度网络. 来看看只有一个神经元的情况,然后才是深度网络. 单个神经元可能有4个输入特征,从\(x_{1}\)到\(x_{4}\), ...