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, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN 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 (<=104), 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 107.

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 <cmath>
using namespace std; int exitPort[]; //开始少一个0,可见细心的重要性, 10^5并不是10005,不是五位数谢谢 int main()
{
int N;
cin >> N; exitPort[] = ; int sum = ;
for(int i = ; i <= N + ; i++)
{
int road;
cin >> road;
sum += road;
exitPort[i] = sum;
} int M;
cin >> M;
while(M--)
{
int in, out;
cin >> in >> out; if(sum - abs(exitPort[out] - exitPort[in]) > abs(exitPort[out] - exitPort[in])) //由于写成了abs(sum - exitPort[out] + exitPort[in]) 有一个点一直没过
{
cout << abs(exitPort[out] - exitPort[in]) << endl;
}
else
cout << sum - abs(exitPort[out] - exitPort[in]) << endl;
} return ;
}
这题唯一的收获就是定义数组的时候最好不要用exit做名称,有的OJ来说, exit是内置的变量,可能会有编译错误,因此修改为exitPort

PAT 1046的更多相关文章

  1. PAT 1046 Shortest Distance

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

  2. PAT 1046 划拳(15)(代码)

    1046 划拳(15)(15 分) 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢 ...

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

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

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

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

  5. PAT 1046. 划拳(15)

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

  6. PAT 1046 划拳

    https://pintia.cn/problem-sets/994805260223102976/problems/994805277847568384 划拳是古老中国酒文化的一个有趣的组成部分.酒 ...

  7. PAT——1046. 划拳

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

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

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

  9. PAT Basic 1046

    1046 划拳 (15 分) 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输 ...

随机推荐

  1. php的empty()和isset()用法

    共同点: 1.都可以判定一个变量是否为空: 2.都返回boolean类型,即true或false. 区别: 1.isset()用来检测变量是否设置,只能用于变量,因为传递任何其它参数都将造成解析错误. ...

  2. Couchbase 中的分布式储存

    Couchbase 是一个具有高性能.可扩展性和可 用性强的数据库引擎.它可以让开发人员通过 NoSQL 的键值存储(二进制或者JSON)或者使用 N1QL 的形式对数据进行操作(N1QL 是非常类似 ...

  3. C语言在open() FIFO文件的时候卡住了

    需要注意的是,open打开FIFO文件的时候,open函数里的flag参数 O——NONBLOCK: 置位:  如果“只写”方式打开文件,写进程会阻塞直到有一个读进程来读这个FIFO管道.就是说:没有 ...

  4. Selenium Web 自动化 - 项目持续集成

    Selenium Web 自动化 - 项目持续集成 2017-02-13 目录 1环境准备  1.1 安装git  1.2 安装jenkins  1.3 安装jenkins插件  1.4 jekins ...

  5. .NET的SqlHelper应用代码

    首先需要引用命名空间 ,同时也需要右击'引用' --> '添加引用' --> '程序集' --> '框架' --> 'System.Configuration',SqlHelp ...

  6. 1692: [Usaco2007 Dec]队列变换(BZOJ1640强化版)

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 682  Solved: 280[Submit][Sta ...

  7. ubuntu 虚拟机vm virtualbox 不能打开 win7

    ubuntu某方面总有些不便,下载个虚拟机装个win7 但是第二次打开的时候就出现了安装是的场景: 原因很简单: 是因为安装了之后没有把win7的镜像文件移除,每次打开时会检测cd/dvd文件/(is ...

  8. Eclipse使用Maven创建Web项目+整合SSM框架

    一.准备环境: maven:apache-maven-3.2.3 jdk:jdk1.8.0_25 tomcat:tomcat-9.0 二.配置Maven.jdk 1.Window——>Prefe ...

  9. UWP--MVVM简单计算器

    namespace LBI.DataBinding { /// <summary> /// 可用于自身或导航至 Frame 内部的空白页. /// </summary> pub ...

  10. 用递归的方式处理数组 && 把递归方法方法定义到数组的原型上 (这是一次脑洞大开的神奇尝试)

    在 javascript 里,如果我们想用一个函数处理数组 (Array) 中的每个元素,那我们有很多种选择,最简单的当然就是用自带的 forEach 函数(低版本也可以使用 lodash 中的 fo ...