【题意】
给出一个环相邻两点之间的距离,求给定的节点对之间的最短距离
【思路】
记录下环的总长和各个节点到第一个节点的距离dist,然后判断两节点i、j的距离就变成求min{|dist[i]-dist[j]|, m-
|dist[i]-dist[j]|}
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char const *argv[])
{
vector<int> dist;
int n,m,totalLen;
cin >> n;
dist.assign(n,0);
totalLen = 0;
for(int i=1; i<=n; i++){
cin >> dist[i%n];
totalLen += dist[i%n];
dist[i%n] += dist[i-1];
}
cin >> m;
while(m--){
int a,b;
cin >> a >> b;
int tmp = dist[b-1]-dist[a-1];
if(tmp<0){
tmp = -tmp;
}
if(2*tmp>totalLen){
tmp = totalLen-tmp;
}
cout << tmp << endl;
}
system("pause");
return 0;
}