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. 【mac版】前端开发工具整理

    整理了一些日常工作中涉及到的工具,下载链接:https://pan.baidu.com/s/1kUD60a7#list/path=%2F 编辑器 Webstorm 软件下载及常用操作 webstorm ...

  2. Java日志工具之java.util.logging.Logger

    今天总结下JDK自带的日志工具Logger,虽然它一直默默无闻,但有时使用它却比较方便.更详细的信息可以查看JDK API手册,本文只是简单示例入门. 创建Logger 我们可以使用Logger的工厂 ...

  3. SQL Sever数据库中 T-sql语句的使用(增、删、改、查)

    SQL中的增.删.改.查 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert ...

  4. C#7的9个新语法

    一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...

  5. 深度剖析Spark分布式执行原理

    让代码分布式运行是所有分布式计算框架需要解决的最基本的问题. Spark是大数据领域中相当火热的计算框架,在大数据分析领域有一统江湖的趋势,网上对于Spark源码分析的文章有很多,但是介绍Spark如 ...

  6. Jmeter函数引用和函数重定向

    在jmeter中的[选项]中选择[函数助手对话框]---这些函数可以高速有效的帮助我们开展自动化编写与校验!!!!!! 如图: 重点!!!本章的侧重点不讲函数的具体使用,函数具体的使用与java类似, ...

  7. Django通用视图执行过程

    使用通用视图后,Django请求处理过程(以ListView为例):在我们自定义的视图中: class IndexView(ListView): template_name = 'blog/index ...

  8. MongoDB一般安装

    MongoDB一般安装 1.首先到官网(http://www.mongodb.org/downloads )下载合适的安装包,目前的最新版本为2.6 安装包有zip和msi格式的,这里推荐下载zip格 ...

  9. 防火墙之netfailt、iptables详解

    [TOC] Iptables入门 # 1.iptables介绍 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成. netfilter 组件也称为 ...

  10. windows phone 8.1常用启动器实例

    ---恢复内容开始--- 小梦今天给大家分享一下windows phone 8.1常用启动器实例,包括: 电话启动器 短信启动器 邮件启动器 添加约会|备忘到日历 地图启动器 地图路线启动器 wind ...