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.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3]), followed by N integer distances D​1​​ D​2​​ ⋯ D​N​​, where D​i​​is the distance between the i-th and the (-st exits, and D​N​​ is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (≤), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 1.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9
3
1 3
2 5
4 1

Sample Output:

3
10
7
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100005 int a[MAXN];
ll dis[MAXN] = {}; int main(){
int n;cin >> n;
ll sum = ;
for(int i=;i < n;i++){
cin >> a[i];sum+=a[i];
dis[i+] = sum;
}
int t;cin >> t;
int cnt = ;
while(t--){ int x,y;cin >> x >> y;
if(x == y){cout << << endl;continue;}
if(x > y)swap(x,y);
ll sum1 = dis[y-] - dis[x-]; ll sum2 = dis[x-] - dis[] + dis[n] - dis[y-]; cout << min(sum1,sum2) << endl; }
return ;
}

两点之间距离是距0的距离之差,线段树和树状数组差不多就这个性质吧。

 

PAT 1046 Shortest Distance的更多相关文章

  1. PAT 1046 Shortest Distance[环形][比较]

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  2. pat 1046 Shortest Distance(20 分) (线段树)

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  5. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  6. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  7. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  8. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  9. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

随机推荐

  1. K8S 安装笔记

    1. 准备CentOS7环境 #关闭防火墙 # systemctl disable firewalld # systemctl stop firewalld #安装etcd, kubernetes(会 ...

  2. ARM伪指令

    1.伪指令是什么 ARM伪指令不是ARM指令集中的指令,只是为了编程方便人们定义了伪指令. 在汇编时这些指令将会被等效的ARM指令或arm指令的组合代替. 编程时可以像其他ARM指令一样使用伪指令,区 ...

  3. _ai_gameobject

  4. java常用技术名词解析

    1.1 token Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便 将此Token返回给客户端,以后客户端只需带上这个Token前来请求数 ...

  5. C#双缓冲代码

    private void Form1_Load(object sender, EventArgs e) { //在窗体加载的时候 解决窗体闪烁问题 //将图像绘制到缓冲区减少闪烁 this.SetSt ...

  6. iterrows(), iteritems(), itertuples()对dataframe进行遍历

      iterrows(): 将DataFrame迭代为(insex, Series)对. itertuples(): 将DataFrame迭代为元祖. iteritems(): 将DataFrame迭 ...

  7. cin 与 getchar 中的坑

    今天在一道题上发现一个坑. 输入三个字符,按以下规则求其平均值. (1)如果是数字0~9,那么直接参与求值: (2)如果是其他字符,则其ASCII码参与求值. 输入 输入数据有多组.第一行是数据的组数 ...

  8. 如何选择合适的 DDoS 防御服务

    如果你没有对自己的站点采取一些必要的保护措施,将会使它直接暴露于 DDoS 攻击的风险下且无任何招架之力.你应该对法国大选日 knocked out 网站被 DDoS 攻击和 2016 年十月份时候美 ...

  9. CC2 条理分明-----AACTP教你谈恋爱

      AACTP是一个神奇的地方,这里可以培养你的自信,培养你的沟通表达能力,培养你的领导管理能力:但是你不知道的是AACTP还可以培养你恋爱的本领.想知道是怎么回事吗?听我给你慢慢说来.我把恋爱分为三 ...

  10. vue子组件,同级组件之间的相互引用,以及传值交互

    博客地址:http://blog.csdn.net/wang_meiwei/article/details/75948844