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. Java8 中的 Optional 相关用法

    基本方法: ofNullable() 为可能 null 的值创建一个 Optional 实例,  然后可以对该实例遍历/过滤, 判断是否存在,或者为空时执行.. ifPresent(...) 如果值存 ...

  2. [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)

    [51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1N​μ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...

  3. 012——C#打开ecxel修改数据(附教程)

    (一)参考文献:[C#]将数据写入已存在的excel文件 C# 导入excel数据,解决关闭excel后不能释放资源的问题 (二)视频教程:https://v.qq.com/x/page/p30063 ...

  4. myeclipse关掉references

    去掉下面两个勾选:

  5. antd 表单获取参数

    wrappedComponentRef是为了把form的值赋值到自定的对象中 <OpenCityForm wrappedComponentRef={(inst)=>{this.cityFo ...

  6. 5-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(,远程升级GPRS内部程序)

    https://www.cnblogs.com/yangfengwu/p/10410202.html 与升级WIFI相同介绍的不再叙述  先看WIFI升级的: ↑ 演示视频: https://www. ...

  7. P1608 路径统计

    题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方,而且他们目前处在标注为 ...

  8. Go by Example-流控制语句之if/else

    Go by Example-流控制语句之if/else Go中的if/else的用法和其他语言没什么区别,在格式要求上保留了类似Python中的一些特性. 基本概念 在条件判断语法 if/else 中 ...

  9. 【零基础】使用Tensorflow实现神经网络

    一.序言 前面已经逐步从单神经元慢慢“爬”到了神经网络并把常见的优化都逐个解析了,再往前走就是一些实际应用问题,所以在开始实际应用之前还得把“框架”翻出来,因为后面要做的工作需要我们将精力集中在业务而 ...

  10. 2018-2019-2 20165209 《网络对抗技术》Exp9: Web安全基础

    2018-2019-2 20165209 <网络对抗技术>Exp9: Web安全基础 1 基础问题回答和实验内容 1.1基础问题回答 (1)SQL注入攻击原理,如何防御? 原理:SQL注入 ...