PAT 甲级 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 D1 D2 ⋯ DN, where Diis the distance between the i-th and the (-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 (≤), 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 分)(前缀和,想了一会儿)的更多相关文章
- 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 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- 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 ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
随机推荐
- 文件读写(一)利用File静态类 System.IO.FileInfo、DirectoryInfo、DriveInfo
提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象. 一.读文件: 1.返回字符串:File.ReadAllText() string readText = ...
- [TypeScript] vs code TSLint常见错误解决方案
TSLint是一个Typescrip{过滤}t验证工具,用于检测代码. TSLint: comment must start with a space (comment-format) 注释必须从一个 ...
- 深入理解JavaScript之实现继承的7种方式
1.原型链继承 核心:将父类的实例作为子类的原型 首先,要知道构造函数.原型和实例之间的关系:构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个原型对象的指针. func ...
- Hadoop集群分布搭建
一.准备工作 1.最少三台虚拟机或者实体机(官网上是默认是3台),我这边是3台 s1: 10.211.55.18 s2: 10.211.55.19 s3: 10.211.55.20 2.安装JDK 3 ...
- 什么是webpack模块化构建工具
百度百科模块化:是指解决一个复杂问题时自顶向下逐层把系统划分成若干模块的过程,有多种属性,分别反映其内部特性. 计算机模块化:一般指的是可以被抽象封装的最小/最优代码集合,模块化解决的是功能耦合问题. ...
- 18 | 为什么这些SQL语句逻辑相同,性能却差异巨大?
在MySQL中,有很多看上去逻辑相同,但性能却差异巨大的SQL语句.对这些语句使用不当的话,就会不经意间导致整个数据库的压力变大. 我今天挑选了三个这样的案例和你分享.希望再遇到相似的问题时,你可以做 ...
- 西门子二次开发--HMI failed to start
一.Sinumerik二次开发错误:HMI failed to start. HMI--SL Framework reported error: GUI Component could not be ...
- 爬虫(三):Requests库的基本使用
一:什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现 ...
- Bacteria (Gym - 101911C)
2018-2019 ACM-ICPC, NEERC, Southern Subregional Contest, Qualification Stage Bacteria Gym - 101911C ...
- webkit vs v8
我们知道不同浏览器用的不同的渲染引擎: Tridend(IE).Gecko(FF).WebKit(Safari,Chrome,Andriod浏览器) 当然 Chrome 重构了一下 WebKit 然后 ...