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 ...
随机推荐
- react相关知识点总结
1 JSX解析的问题 JSX其实是语法糖: 开发环境会将JSX编译成JS代码 react定义的方法,用来解析html,第一个参数是“html元素”,第二个参数是“属性”,第三个参数是其子元素: 所以下 ...
- nextjs —— jsx style 学习记录
作用域 全局 <style global jsx>{` .hero { width: 100%; color: #333; } .title { margin: 0; width: 100 ...
- js onclick事件传参
传字符串参数 var html = "<a href='#' onclick='onedit(""+ row.name + "")';>编 ...
- go语言-变量与常量
变量 一.变量注意事项 变量名首字母大写,可以被其他包访问调用(公有),变量名首字母小写,其他包不能访问和调用(私有) 在同一个域里一个变量只能定义一次,不可重复定义 二.变量的声明的种方式 1.先声 ...
- python 虚拟环境相关命令
1.总是记不住一些关于创建虚拟环境得命令,特在自己得博客里记录一下自己常用得命令: virtualenv -p C:\Python36\python D:\virtual\Envs\AssetScan ...
- MongoDB 查看chunk块大小
使用mongo shell连到mongos执行命令:AllChunkInfo("dbname.cellname",true) 点击(此处)折叠或打开 AllChunkInfo = ...
- 动态menu导航条以及treeview树
1.menu表数据 2.在后台生成html内容后,前台利用nav-h.css生成menu导航条,利用Jquery的treeview插件生成menu树 前台coding: <!DOCTYPE ht ...
- java重载和重写
重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. ...
- 内存原理与PHP的执行过程
一.内存结构 栈区:保存的是变量名(术语:引用),对于cpu来说,读写速度很快 堆区:存储“复杂”的数据,数组.对象.字符串(字符串比较特殊)等 数据段:又分为数据段全局区(用于存储简单的数据,如数字 ...
- shiro 配置注解异常 java.lang.ClassNotFoundException: org.aspectj.util.PartialOrder$PartialComparable
解决方案: pom 文件添加: <!-- 解决shiro注解(shiro 使用 aop) --> <dependency> <groupId>aspectj< ...