环形专题
作者:互联网
Shortest Distance (20)
Link
使用数组长度倍增的方法。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int n,m,x,y;
int d[200010];
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&d[i]);
d[i+n]=d[i];
}
for(int i=2;i<=(n<<1);++i)
d[i]+=d[i-1];
scanf("%d",&m);
while(m--){
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
printf("%d\n",min(d[y-1]-d[x-1],d[x+n-1]-d[y-1]));
}
return 0;
}
标签:iy,专题,20,int,环形,200010,printf,include 来源: https://www.cnblogs.com/preccrep/p/16412357.html