https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424

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, 10^5^]), followed by N integer distances D~1~ D~2~ ... D~N~, where D~i~ is the distance between the i-th and the (i+1)-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 (<=10^4^), 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 10^7^.

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; const int maxn = 1e5 + 10;
int a[maxn], dis[maxn]; int main() {
int N, sum = 0;
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
scanf("%d", &a[i]);
sum += a[i];
dis[i] = sum;
} int T, left, right, temp;
scanf("%d", &T);
for(int i = 1; i <= T; i ++) {
scanf("%d%d", &left, &right);
if(left > right) {
swap(left, right);
}
temp = dis[right - 1] - dis[left - 1];
printf("%d\n", min(temp, sum - temp));
} return 0;
}

  

PAT 甲级 1046 Shortest Distance的更多相关文章

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

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

  2. 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 ...

  3. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  4. PAT甲级——A1046 Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

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

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

  6. PAT 1046 Shortest Distance

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

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

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

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

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

  9. 1046 Shortest Distance (20 分)

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

随机推荐

  1. Vivado 调用自定义IP核

    关于Vivado如何创建自定义IP核有大量的参考文章,这里就不多加阐述了,本文目的主要是解决如何在新建工程中引用其它工程已经自定义封装好的IP核,从而实现自定义IP核的灵活复用. 举个例子,我们的目标 ...

  2. 114. Unique Paths [by Java]

    Description A robot is located at the top-left corner of a m x n grid. The robot can only move eithe ...

  3. 20155217 实验四 Android程序设计

    20155217 实验四 Android程序设计 任务一: 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号. R.java文件是定义该项目所有资源的 ...

  4. 20155223 2016-2017-2《Java程序设计》课程总结

    20155223 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1 预备作业2 预备作业3 第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 ...

  5. 20155304 2016-2017-2 《Java程序设计》第十周学习总结

    20155304 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是 ...

  6. 20155318 2016-2017-2 《Java程序设计》第三周学习总结

    20155318 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 一.类与对象 Clothes c1 = new Clothes();将c1名称参考至新建对象 ...

  7. 20155322 2016-2017-2 《Java程序设计》第9周学习总结

    20155322 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第9周学习的主要内容是课本的第十六.第十七.第十八章,老师的教学指导上主要要求学习以下知识点 ...

  8. apache-kylin-2.5.2-bin-cdh57与cdh-5.13.0集群整合运用

    1.下载kylin最新版apache-kylin-2.5.2-bin-cdh57: 2.解压配置环境变量: export BASE_PATH="/opt/cloudera/parcels/C ...

  9. 八、Django之Models(译)

    模型(Models) 模型是你的数据的唯一的.确定的信息源. 它包含你所储存数据的必要字段和行为. 通常,每个模型对应数据库中唯一的一张表. 基础: 每个模型都是一个Python类,它们都是djang ...

  10. 会声会影2018提示dll文件丢失怎么办?

    一些会声会影2018用户,在安装.使用软件的过程中,会出现dll缺失的提示,导致软件无法打开,那么,出现这一问题要怎么解决.接下来小编为大家具体介绍下两种解决方法. 图1:dll丢失提示 打开会声会影 ...