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

题目描述:

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.

译:你的任务很简单:给定 N 个出口,形成一个简单的圆形公路,你应该说出任意一对出口之间的最短距离。


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.

译:每个输入文件包含一个测试用例,每个用例在第一行中包含一个正整数 N ( 3 ≤ N ≤10 5 ) , 紧跟着 N 个表示距离的整数 D1 D2 ⋯ DN , Di 表示 第 i 个 出口到第 i + 1 个出口之间的距离, DN 表示第 N 个出口到第 1 个出口之间的距离。所有的数字被一个空格分隔。第二行给出一个正整数 M (≤104) , 接下来 M 行,每行包含一对出口的编号,保证出口在 1 ,N之间。题目保证整个环道的距离不超过 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.

译:对于每个测试用例,在 M 行中打印相应那对出口之间的最短距离 。


Sample Input (样例输入):

5 1 2 4 14 9
3
1 3
2 5
4 1

Sample Output (样例输出):

3
10
7

The Idea:

本题的最短距离还算简单。我们只需要一个 数组存储 第 1 个出口 到 第 i 个出口之间的距离。然后求两个出口之间的距离,就变成了简单的减法问题,由于是一个环道,最短距离需要考虑在两个距离之间抉择:a 到 b 的距离 和整个环道的距离 减去 a 到 b 的距离 。


The Codes:

#include<bits/stdc++.h>
using namespace std ;
#define MAX 100010
int sum[MAX] = { 0 } ;
int n , m , t , a , b ;
int main(){
scanf("%d" , &n) ;
for(int i = 1 ; i <= n ; i ++){
scanf("%d" , &t) ;
sum[i] = sum[i-1] + t ; // 计算 第 1 个出口 到 第 i 个出口之间的距离。
}
scanf("%d" , &m) ;
while(m --){
scanf("%d%d" , &a , &b) ;
if(a > b) swap(a , b) ; // 如果 a 大于 b 就交换一下
cout<<min(sum[b - 1] - sum[a - 1] , sum[n] - (sum[b - 1] - sum[a - 1]))<<endl ;
}
return 0;
}

PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  2. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  3. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  4. PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  5. PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  6. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  9. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

随机推荐

  1. 如何使用 js 实现一个 throttle 函数

    如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...

  2. Netty & websockets

    Netty & websockets Netty is a non-blocking I/O client-server framework for the development of Ja ...

  3. k8s部署mysql数据持久化

    在这里我部署mysql的目的是为了后面将上一篇博客docker打包的el-admin镜像部署到k8s上,所以本文主要是部署mysql并实现持久化. 1.将我们的应用都部署到 el-admin 这个命名 ...

  4. 比特币大涨之际,VAST空投火爆来袭!

    昨天特斯拉宣布投资了15亿美元的比特币,消息一出,币圈沸腾,比特币瞬间拉升,突破4万美元关口,现价46000美元!大涨20%,又刷新历史新高! 另外NGK项目热度最高的BGV也是不断刷新历史新高,数据 ...

  5. 智能合约稳定币USDN的价值在哪里?

    近几年来,区块链和数字货币市场快速发展,客观上需要价格相对稳定的交易媒介和贮藏手段,从而推动以链上资产或链下资产抵押型稳定币和算法型稳定币出现,以实现币价相对稳定的数字货币.市场上开始出现了诸如USD ...

  6. JVM性能调优经验总结

    本文转载自JVM性能调优经验总结 说明 调优是一个循序渐进的过程,必然需要经历多次迭代,最终才能换取一个较好的折中方案. 在JVM调优这个领域,没有任何一种调优方案是适用于所有应用场景的,同时,切勿极 ...

  7. 官网GitLab CI/CD英文文档翻译

    在查阅GitLab官网的CI/CD功能说明时,全是英文看起来不方便,通过翻译软件自动翻译后"内容失真",看起来很变扭.查阅了百度上的资料发现很多翻译很老旧,有些甚至是挂羊头卖狗肉. ...

  8. mybatisPlus中的模糊查询问题

    不能查询中文 记得在数据库的配置中写明编码格式characterEncoding=utf-8

  9. 微信小程序:Navigator导航组件

    导航组件:类似超链接标签. url:要跳转的页面路径,可以放绝对路径,也可以放相对路径,绝对路径指从pages作为根目录开始找到你要的页面. 找到你要找的页面的相对地址的方法:在vscode中,该页面 ...

  10. NodeJs 入门到放弃 — 入门基本介绍(一)

    码文不易啊,转载请带上本文链接呀,感谢感谢 https://www.cnblogs.com/echoyya/p/14450905.html 目录 码文不易啊,转载请带上本文链接呀,感谢感谢 https ...