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. hdu 3836 Equivalent Sets trajan缩点

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  2. [原][粒子特效][spark]发射器emitter

    深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html group添加emitter的方式: eimtter: 上图是spark源码 ...

  3. [转][linux]简单的linux下的tcp/udp

    转自:https://blog.csdn.net/cabing2005/article/details/53068880 详细函数以及参数解释请看原链接. windows下的tcp/udp参考:htt ...

  4. Spring 的@Required注释

    本例子源于:W3Cschool,在此做一个记录 @Required注释为为了保证所对应的属性必须被设置,@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean ...

  5. Qt5获取本机网络信息

    获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...

  6. Axure 元件焦点的控制

    讲解如何控制光标的位置,主要学习了以下三种场景: 1.点击邮箱或者密码时,光标分别自动定位到email.password的文本框处: 2.还有将密码对应的文本框的内容设置为密码的格式(····) 3. ...

  7. php oracle数据库clob和nclob字段

    php oracle数据库clob和nclob字段 nclob类型 1.nclob不能使用php的stream_get_contents来获取数据库的资源内容, 2.并且nclob只能使用to_cha ...

  8. Django初始化之基本操作

    1.指定要安装的Django版本 C:\Users\win7>pip install Django==1.11.8 2.查看安装的django版本 C:\Users\win7>pip sh ...

  9. webpack基础概念

    中文文档:https://webpack.docschina.org/concepts/ 一个现代 JavaScript 应用程序的静态模块打包器(static module bundler). 在 ...

  10. 关于如何获取Google 官方 NavgationView中的控件的方法

    最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...