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,105]), followed by Ninteger 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.
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
分析: 题目意思是给了一个环,已经环的每条边(公路)的长度,现给出起点和终点,求出起点和终点之间的最短路径长度,即求顺时针和逆时针行驶的距离最小值。
先求顺时针的距离,而逆时针距离=sum-顺时针距离;
另外暴力求解测试点3会超时,因为每次遍历数组,最大情况10^5个顶点,若有10^4个查询,则一共10^9,对于100ms的时限不能承受。
可以在输入距离时用dis数组记录从1顺时针出发到各顶点的距离。
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-23-19.12.21
* Description : A1046
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
int a[maxn],dis[maxn];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
;
scanf("%d",&n);
;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
dis[i]=sum;
}
scanf("%d",&m);
;i<m;i++){
,ans2=;
scanf("%d%d",&st,&ed);
if(st>ed) swap(st,ed);
ans1=dis[ed-]-dis[st-];
ans2=sum-ans1;
printf("%d\n",min(ans1,ans2));
}
;
}
1046 Shortest Distance (20 分)的更多相关文章
- 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 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- 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 ...
- 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 (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- 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 ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- 1046 Shortest Distance (20)
#include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...
- PAT (Advanced Level) 1046. Shortest Distance (20)
处理一下前缀和. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
随机推荐
- <NET CLR via c# 第4版>笔记 第17章 委托
17.1 初识委托 .net 通过委托来提供回调函数机制. 委托确保回调方法是类型安全的. 委托允许顺序调用多个方法. 17.2 用委托回调静态方法 将方法绑定到委托时,C# 和 CLR 都允许引用类 ...
- nginx对上传文件大小的限制
前几天项目组的一个小伙子过来跟我说,他本地上传的文件没问题,但是在测试环境上测试的时候上传失败. 后来发现时nginx对上传文件的大小做了限制,默认是1M,那么按照需求我们更改了限值为20M vi c ...
- zabbix监控系统的配置
一.zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...
- ImportError: cannot import name 'path'
ImportError: cannot import name 'path' django的版本问题 先卸载 django1.11.14 重新安装最新的django2.0.7
- Kotlin Reference (十二) Extensions
most from reference Kotlin与C#和Gosu类似,提供了扩展一个新功能的类,而不必继承类或使用任何类型的设计模式,如Decorator(装饰者模式).这是通过称为扩展的特殊声明 ...
- 对抗网络之目标检测应用:A-Fast-RCNN
对抗网络之目标检测应用:A-Fast-RCNN 论文:A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection ...
- VC++实现解析快捷方式
VC++实现解析快捷方式: http://blog.csdn.net/mydreamremindme/article/details/9826745
- Android Kernel save defalut config
/********************************************************************************* * Android Kernel ...
- linux-锁屏时间设置
系统:ubuntu16.04 操作步骤 设置setting -> System Settings -> Brightness&lock, 在界面中选择想要的设置即可: 参考 1.百 ...
- HDU1081 最大字段和 压缩数组(单调队列优化)
最大字段和题型,推荐做题顺序: HDU1003 HDU1024 HDU1081 ZOJ2975 ZOJ2067 #include<cstdio> #include<cstdlib& ...