PAT Advanced 1046 Shortest Distance
程序员文章站
2022-06-07 09:46:40
...
#include<stdio.h>
#include<vector>
using namespace std;
int main(){
int n;
scanf("%d",&n);
vector<int> dis(n);
int sum=0;
vector<int> porder(n);
vector<int> rorder(n);
for(int i=0;i<n;i++){
porder[i] = sum;
int v;
scanf("%d",&v);
sum+=v;
dis[i]=v;
}
int rsum=0;
for(int i=n-1;i>=0;i--){
rsum+=dis[i];
rorder[i]=rsum;
}
int m;
scanf("%d",&m);
for(int i=0;i<m;i++){
int st,ed;
scanf("%d %d",&st,&ed);
int little = st<ed?st:ed;
int large = st<ed?ed:st;
int partSum= porder[little-1] + rorder[large-1];
int res = partSum<sum-partSum?partSum:sum-partSum;
// printf("sum:%d psum:%d sum-psum:%d res:%d\n",sum,psum,sum-psum,res);
printf("%d\n",res);
}
}
上一篇: Newton迭代,割线法
下一篇: 1050. 螺旋矩阵(25) C++