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 <iostream>
#include <vector>
using namespace std;
int N, M;
int main()
{
cin >> N;
int num, a, b;
vector<int>sum(N + , );
for (int i = ; i <= N; ++i)
{
cin >> num;
if (i == N)
sum[] = sum[N] + num;
else
sum[i + ] = sum[i] + num;
}
cin >> M;
for (int i = ; i < M; ++i)
{
cin >> a >> b;
if (a > b)
swap(a, b);
int d1 = sum[b] - sum[a];
int d2 = sum[] - sum[b] + sum[a] - sum[];
cout << (d1 < d2 ? d1 : d2) << endl;
}
return ;
}

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

  1. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really 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 ...

  3. PAT A1046 Shortest Distance

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

  4. 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甲 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 ...

  6. A1046 Shortest Distance (20)(20 分)

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

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

  8. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  9. A1046. Shortest Distance(20)

    17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...

随机推荐

  1. Linux课程---13、linux中任务计划介绍(任务计划分类)

    Linux课程---13.linux中任务计划介绍(任务计划分类) 一.总结 一句话总结: 1.一次性任务计划:at 2.周期性任务计划:crontab 1.linux中如何添加一次性任务计划? at ...

  2. swiper在loop模式,当轮播到最后一张图时候,做其他事件

    1.引入文件: <link rel="stylesheet" href="css/swiper.min.css"> <script src=& ...

  3. docker registry (私有仓库)

    首先要导入registry的镜像文件 1.普通的registry 一条命令创建一个仓库 docker run -d -p 5000:5000 --restart=always --name regis ...

  4. 19.SimLogin_case01

    什么是模拟登录? 要抓取的信息,只有在登录之后才能查看.这种情况下,就需要爬虫做模拟登录,绕过登录页. cookies和session的区别: cookie数据存放在客户的浏览器上,session数据 ...

  5. (转)NodeJS收发GET和POST请求

    NodeJS收发GET和POST请求 目录: 一 express框架接收 二 接收Get 三 发送Get 四 接收Post 五 发送Post 一 express框架接收 1 2 3 4 5 app.g ...

  6. 小程序跳转传参参数值为url时参数丢失

    通过先encodeURIComponent,取到值以后再decodeURIComponent,拼接参数正常传递 A页面 switch: function (e) { var aa = 'UNNZVUf ...

  7. 时间复杂度 - Convert 计算次数 TO 时间复杂度

    我们假设计算机运行一行基础代码需要执行一次运算. int aFunc(void) { printf("Hello, World!\n"); // 需要执行 1 次 return 0 ...

  8. C++ Builder获取系统文件的路径

    取得路径的程序:(注意红色字体,由于博客显示问题,所以中间加了空格,大家自己把空格去掉即可) // -------------------------------------------------- ...

  9. Duilib 入门教程: 怎么创建一个自定义的窗口

    一直想找一个好用UI 界面库,看过Direct UI,也想过 金山的界面库,后来找到了这个Duilib 现在的软件界面很多都是利用XML 来布局和定位. 像迅雷7,QQ,金山卫士等 [html] vi ...

  10. NOIP 2017 提高组 day1t2 时间复杂度

    P3952 时间复杂度 标签 NOIp提高组 2017 时空限制 1000ms / 128MB 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂 ...