题目大意: 给出一个循环数字序列,求两个给定数字之间距离的最小值。
CODE ⇩⇩⇩
#include <bits/stdc++.h>
using namespace std;
int dist[100005];
int main() {
int n,m,a,b,sum = 0;
cin>>n;
for(int i = 1;i <= n;i++) cin>>dist[i],sum += dist[i];
cin>>m;
while(m--){
cin>>a>>b;
if(a > b) swap(a,b);
int nx = accumulate(dist+a,dist+b,0);
cout<<min(nx,sum-nx)<<endl;
}
return 0;
}
本文介绍了一种解决循环数字序列中两数间最小距离问题的高效算法。通过积累数组元素和计算不同路径长度,该算法能快速找到最短路径。

347

被折叠的 条评论
为什么被折叠?



