[PAT] Class 1046 Shortest Distance (20 points)

Meaning of the questions:

Enter a positive integer N (<= 1e5), represents the number of outlets, the next input N represents a positive integer from this exit to the next exit. Then enter a positive integer M (<= 10000), representative of the number of queries, each query input port number two, the output of the minimum distance between them.

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int dis[100007],sum[100007];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>dis[i];
sum[i]=sum[i-1]+dis[i];
}
int q;
cin>>q;
for(int i=1;i<=q;++i){
int u,v;
cin>>u>>v;
if(v<u)
swap(u,v);
int x=sum[v-1]-sum[u-1];
int y=sum[n]-x;
cout<<min(x,y)<<"\n";
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11605845.html