The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int>dis(n+1);//声明一个初始大小为n+1的向量
int sum=0,l,r,k;
for(int i=1;i<=n;i++)
{
int t;
cin>>t;//输入i到i的下一个结点的距离
sum+=t;
dis[i]=sum;
}
cin>>m;//输入m个询问
for(int i=0;i<m;i++)
{
cin>>l>>r;//输入开始结点和结束结点
if(l>r)swap(l,r);int t=dis[r-1]-dis[l-1];
cout<<min(t,sum-t)<<endl;
}
return 0;
}