题意:给定 n 个地方,然后再给 m 个任务,每个任务必须在规定的地方完成,并且必须按顺序完成,问你最少时间。

析:没什么可说的,就是模拟,记录当前的位置,然后去找和下一个位置相差多长时间,然后更新当前位置即可。

代码如下:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 5;
typedef long long LL;

int main(){
    int n, m, x;
    while(cin >> n >> m){
        LL ans = 0;
        int pos = 1;
        for(int i = 0; i < m; ++i){
            cin >> x;
            if(x == pos)  continue;
            if(x > pos){
                ans += x - pos;
            }
            else ans += x + n - pos;
            pos = x;
        }
        cout << ans << endl;
    }
    return 0;
}

 

相关文章:

  • 2019-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-09-16
  • 2021-07-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-26
  • 2021-06-25
  • 2021-12-19
  • 2022-01-29
  • 2021-10-03
  • 2021-10-13
相关资源
相似解决方案