leetcode-master/problems/kamacoder/0128.小美走公路.md

36 lines
953 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 小美走公路
在处理环形情况的时候,很多录友容易算懵了,不是多算一个数,就是少算一个数。
这里这样的题目,最好的方式是将 两个环展开,首尾相连,这样我们就可以通过 直线的思维去解题了
两个注意点:
1. x 可以比 y 大,题目没规定 x 和y 的大小顺序
2. 累计相加的数可能超过int
```CPP
#include <iostream>
#include <vector>
using namespace std;
int main () {
int n;
cin >> n;
vector<int> vec(2* n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> vec[i];
vec[n + i] = vec[i];
}
int x, y;
cin >> x >> y;
int xx = min(x ,y); // 注意点1x 可以比 y 大
int yy = max(x, y);
long long a = 0, b = 0; // 注意点2相加的数可能超过int
for (int i = xx; i < yy; i++) a += vec[i];
for (int i = yy; i < xx + n; i++ ) b += vec[i];
cout << min(a, b) << endl;
}
```