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. VS2010-MFC(图形图像:CDC类及其屏幕绘图函数)

    转自:http://www.jizhuomi.com/software/244.html 上一节讲了文本输出的知识,本节的主要内容是CDC类及其屏幕绘图函数. CDC类简介 CDC类是一个设备上下文类 ...

  2. nginx实用配置用例

    vue项目部署及后台api访问 nginx.conf # vue本地项目配置 ... server { listen 8000; server_name localhost; root /.../di ...

  3. <Python基础>字符串的基本操作

    s = 'abCDeFg aBcDea' print(s.find('b')) #通过元素查找索引,找不到返回-1 print(s.index('b')) #通过元素查找索引,找不到报错(会报错,基本 ...

  4. HDU 1147 /// 判断两线段相交

    题目大意: 给定n条线段的端点 依次放上n条线段 判断最后在最上面(不被覆盖)的线段有哪些 到当前线段后 直接与之前保存的未被覆盖的线段判断是否相交就可以了 #include <cstdio&g ...

  5. new linux setup, yum command

    7  yum list 9  cd /etc/yum.repos.d/ 55  history | grep yum 56  yum -y list screen* 57  yum -y instal ...

  6. Linux-c对一个十六进制数的某一位取反

    enum SWITCH_FLAG { SWITCH_ALL_FLAG = , SWITCH_WEB_FLAG = , …… } unsigned int switch_by_bit_value = 0 ...

  7. selenium学习笔记11——driver.get(url) 页面加载时间太长

    在执行自动化测试用例过程中,发现因为网络慢或其他原因导致driver.get(url) 时,页面一直在加载,页面没有加载完成就不会去继续执行下面的动作,但是实际上需要操作的元素已经加载出来了. 解决方 ...

  8. 廖雪峰Java11多线程编程-3高级concurrent包-5Atomic

    Atomic java.util.concurrent.atomic提供了一组原子类型操作: 如AtomicInteger提供了 int addAndGet(int delta) int increm ...

  9. 「题解」:[AHOI2013]作业

    问题: 作业 时间限制: 10 Sec  内存限制: 512 MB 题面 题目描述 此时己是凌晨两点,刚刚做了Codeforces的小A掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是 ...

  10. TokuDB安装

    安装TokuDB 1, 创建mysql数据目录 #顺便把临时目录创建好 mkdir -p /data/mysql/tmp groupadd -r mysql useradd -g mysql -r - ...