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. uni-app 下拉至指定高度固定view

    uni.createSelectorQuery().select(‘#salyt’).boundingClientRect(function(rects){ console.log(rects) va ...

  2. WPF自定义命令

    WPF的自定义命令实现过程包括三个部分,定义命令.定义命令源.命令调用,代码实现如下: public partial class MainWindow : Window { public MainWi ...

  3. PL/SQL轻量版(三)——游标与异常处理

    一.游标 1.概念 游标是一个 指向上下文的句柄( handle) 或指针.通过游标,PL/SQL 可以控制上下文区和处理语句时上下文区会发生些什么事情. 2.游标处理 处理显式游标 主要包含以下四个 ...

  4. day4 边缘检测Canny

    1.canny边缘检测 # coding=utf-8 import cv2 import numpy as np filename = 'woman.JPEG' #读入图像,以灰度格式 img = c ...

  5. CF543E Listening to Music

    题面 空间只有$64\text{MB}$!!! 题解 (据说正解是毒瘤分块套分块) 按照权值从大到小排序,对所有能够覆盖到它的区间的左端点打个标记 按照值域建一棵主席树就可以了 区间查询最大值,用$m ...

  6. 1178: [Apio2009]CONVENTION会议中心

    1178: [Apio2009]CONVENTION会议中心 https://lydsy.com/JudgeOnline/problem.php?id=1178 分析: set+倍增. 首先把所有有包 ...

  7. Linux系统运维基础管理命令总结

    1.查看系统负载命令:w.uptime [root@localhost ~]# w :: up days, :, user, load average: 0.00, 0.01, 0.05 USER T ...

  8. ps 图层解锁后变成全格子(全透明)的解决方法

    其实是因为同时打开了好几个ps文件正在编辑中,所以解决方法就是重启ps,然后单独编辑一个文件,解锁后就不会再出现这种情况能,就能正常编辑了

  9. WPF RegisterAttached ListBoxItem(附加属性传递到Item)

    /// <summary> /// Controls的附加属性 /// </summary> public class ControlsAttached : Dependenc ...

  10. macOS中启动Tomcat提示Cannot find ./catalina.sh

    首先查看Tomcat目录下是否存在catalina.sh,如果文件不存在,文件丢失,最好的方式是重装Tomcat Tomcat官网:http://tomcat.apache.org/ 如果文件存在,那 ...