【1046】Shortest Distance (20 分)

简介: 【1046】Shortest Distance (20 分)【1046】Shortest Distance (20 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;
const int MAXN=100005;
int dis[MAXN],A[MAXN]; 
//dis[i]表示1号结点按顺时针到达i号结点顺时针的下一个结点的distance
//A[i]表示i号与i+1号结点的距离
//key:每次查询left-》right,取dis(left,right)和sum-dis(left,right)最小值
int main(){   
  int sum=0,query,n,left,right;//记得sum要初始化。。
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d",&A[i]);
    sum+=A[i]; //累加sum
    dis[i]=sum;//预处理dis数组
  }
  scanf("%d",&query);
  for(int i=0;i<query;i++){  //query个查询
    scanf("%d%d",&left,&right); //left->right
    if(left>right)  swap(left,right);  //left>right时交换
    int temp=dis[right-1]-dis[left-1];
    printf("%d\n",min(temp,sum-temp));
  }
  system("pause");
    return 0;   
}
相关文章
|
机器学习/深度学习 算法 搜索推荐
曼哈顿距离(Manhattan distance)
曼哈顿距离(Manhattan distance),也称为城市街区距离(City block distance)或L1距离(L1 distance),是两个点在标准坐标系上的绝对值距离之和。
830 4
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
65 0
|
SQL 关系型数据库 MySQL
LeetCode 613. Shortest Distance in a Line
LeetCode 613. Shortest Distance in a Line
99 0
【1142】Maximal Clique (25分)【有点问题】
【1142】Maximal Clique (25分)【有点问题】 【1142】Maximal Clique (25分)【有点问题】
80 0
LeetCode之Hamming Distance
LeetCode之Hamming Distance
121 0
1046. Shortest Distance (20)
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.
961 0