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]), 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

题意:

  给出一个环形的高速公路,其中有N个出口,第Di​个出口是i到i-1的距离,而DN是N到1 的距离。给出任意两个出口,计算两者的最短距离。

题解:

显而易见,这是一个循环队列,计算距离需要考虑两个方向,但是如果直接遍历的话,时间复杂度为O(n2) O(n^2)O(n 2)这个数据量会超时(第三个测试点),所以我们需要考虑,如何优化这个距离计算过程。不妨考虑,计算每个出口两个方向的累加距离,这样计算两者之间的距离的时候,直接做加减即可,时间复杂度为O(n) O(n)O(n)。

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int n;
int a[];
int s1[];
int s2[];
int main()
{
cin>>n;
memset(s1,,sizeof(s1));
memset(s1,,sizeof(s2));
for(int i=;i<=n;i++){
cin>>a[i];
s1[i+]=s1[i]+a[i];
}
for(int i=;i<=n;i++){
s2[i+]=s2[i]+a[n-i+];
}
/*for(int i=1;i<=n;i++){
cout<<i<<" s1 "<<s1[i]<<endl;
cout<<i<<" s2 "<<s2[i]<<endl;
}*/
int m;
int u,v;
cin>>m;
for(int i=;i<=m;i++){
cin>>u>>v;
if(u>v){
int temp=v;
v=u;
u=temp;
}
cout<<min(s1[v]-s1[u],s2[n+-v]+s1[u])<<endl;//两个方向选最大
}
return ;
}

PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)的更多相关文章

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

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

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

  3. 1046 Shortest Distance (20 分)

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

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

  5. 1046 Shortest Distance (20分)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  6. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

  7. 【PAT甲级】1046 Shortest Distance (20 分)

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  8. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  9. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

随机推荐

  1. 0020SpringBoot使用SpringCloud中的eureka实现远程调用

    要实现远程调用,主要需要三个module:一个注册中心.一个服务提供者.一个服务消费者,然后进行各自的配置和编码,详细内容如下: 1.建一个空的project,创建3各module a.注册中心模块 ...

  2. Robot Framework--接口测试环境搭建

    1.安装requests库 (robotframework-requests依赖这个request http client) 执行pip install requests 2. 安装requestLi ...

  3. Jquery Ajax跨域访问

    一.同源策略 二.跨域的集中方法: 1.服务器端发送请求,服务器作为中继代理(此方法不理解) 2.iframe 3.script标签 通过动过动态生成script标签,并将src指向目标源的方式(im ...

  4. tomcat实现文件上传下载

    实现下载 修改server.xml修改web.xml   实现上传 实现客户端的上传post请求代码实现 实现服务端的处理   小结         实现下载 实现下载需要  - 修改Tomcat中的 ...

  5. js与json的区别,json的概述,json与面向对象,json与对象的转换

    <script> //js与json的区别,json的概述,json与面向对象,json与对象的转换 //json的概述:json(javascript object Notation,j ...

  6. 【安卓笔记】Android保持Session

    简介: Session的概念在Android开发中遇到概率还是比较少,之前遇到一个项目的登陆接口返回不是Token,而且自己需要和服务端保持是Session,虽然这种情景很少,但是有时候后端有这种要求 ...

  7. 「学习笔记」FFT及NTT入门知识

    前言 快速傅里叶变换(\(\text{Fast Fourier Transform,FFT}\) )是一种能在\(O(n \log n)\)的时间内完成多项式乘法的算法,在\(OI\)中的应用很多,是 ...

  8. linux系列(十一):nl命令

    1.命令格式: nl [选项] [文件] 2.命令功能: nl(Number of Lines) 将指定的文件添加行号标注后写到标准输出.如果不指定文件或指定文件为"-" ,程序将 ...

  9. linux系列(二十二):tar命令

    1.命令格式 tar[必要参数][选择参数][文件] 2.命令功能 用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的 3.命令参数 必要参数: -A 新增压缩文件到已存在的压缩 - ...

  10. spring boot 扫描 其他jar包里面的 mapper xml

    启动类配置扫描 goods.mapper为当前项目mapper路径 ,common.mpper为其他jar包. 1. 2.mybatis.mapper-locations=classpath*:map ...