加一倍,变成一个环就行了,注意处理细节问题。
#include <iostream> #include <queue> #include <map> #include <set> #include <cmath> #include <cstring> #include <algorithm> #include <iomanip> #include <stack> #include <numeric> #include <ctime> #include <string> #include <bitset> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; const ll N = 1e5 + 5, mod = 1e9 + 7, inf = 0x3f3f3f3f; int n, m; ll a[N << 1]; void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[n + i] = a[i]; } for (int i = 1; i <= 2 * n; i++) { a[i] += a[i - 1]; } int s, t; cin >> s >> t; if (s > t)swap(s, t); cout << min(a[t-1] - a[s-1], a[s + n - 1] - a[t - 1]); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; //cin >> t; while (t--) { solve(); } return 0; }