PAT1046: Shortest Distance
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 N integer 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 思路 1.直接用累加求和,结果最后一项测试用例超时。
2.开一个二维数组暴力枚举出所有可能的情况,检测时直接输出对应情况,结果内存超了(最坏情况数组大小100001 * 100001)。
3.保存每一个目标点到起始点的road[i],那么任意两个点s,e的距离可以通过road[s]-road[e]计算出来,而不用再去一步步累加,所以不会超时。而且这种解决方案最坏情况开辟的数组大小为100001,不会超内存。 代码
#include<iostream>
#include<vector>
#include<math.h>
using namespace std; int main()
{
int N;
while(cin >> N)
{
vector<int> road(N + ,);
int sum = ;
for(int i = ;i <= N;i++)
{
int value;
cin >> value;
road[i + ] = road[i] + value;
sum += value;
}
int M;
cin >> M;
while(M--)
{
int s,e;
cin >> s >> e;
int path = abs(road[s] - road[e]);
cout << min(path,sum - path) << endl;
}
}
}
PAT1046: Shortest Distance的更多相关文章
- pat1046. Shortest Distance (20)
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- [CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离
18.5 You have a large text file containing words. Given any two words, find the shortest distance (i ...
- [Locked] Shortest Distance from All Buildings
Shortest Distance from All Buildings You want to build a house on an empty land which reaches all bu ...
- maximum shortest distance
maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- LeetCode 613. Shortest Distance in a Line
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
随机推荐
- C语言函数strstr()分析及实现
原型:char *strstr(const char *str1, const char *str2); #include<string.h> 找出str2字符串在str1字符串中第一次出 ...
- MFC中使用SDL播放音频没有声音的解决方法
本文所说的音频是指的纯音频,不包含视频的那种. 在控制台中使用SDL播放音频,一般情况下不会有问题. 但是在MFC中使用SDL播放音频的时候,会出现没有声音的情况.经过长时间探索,没有找到特别好的解决 ...
- Android实现无线调试自己的应用
开发Android的朋友都知道,真机调试需要把手机与PC相连,然后把应用部署到真机上进行安装和调试.长长的USB线显得很麻烦,而且如果需要USB接口与其他设备连接的话显得很不方便.今天介绍一种不通过U ...
- 对MBProgressHUD第三方进行源码分析
GitHub源码地址,及时更新:iOS-Source-Code-Analyze MBProgressHUD是一个为iOS app添加透明浮层 HUD 的第三方框架.作为一个 UI 层面的框架,它的实现 ...
- 增加AP INVOICE 行&分配行
-- 增加行 DECLARE v_row_id VARCHAR2(1000); v_line_number number; g_user_id CONSTANT NUMBER := fnd_globa ...
- 面试之路(29)-TCP流量控制和拥塞控制-滑动窗口协议详解
拥塞: 拥塞发生的主要原因在于网络能够提供的资源不足以满足用户的需求,这些资源包括缓存空间.链路带宽容量和中间节点的处理能力.由于互联网的设计机制导致其缺乏"接纳控制"能力,因此在 ...
- 近期ubuntu 14.04 cpu占用高排障
近期linux使用总是cpu达到满值, 双核cpu其中一个核总是100%,另一个核正常.top之发现输入法框架fcitx满载,直接kill之,发现搜狗输入法不能用了,随即输入如下命令: fcitx f ...
- python笔记--1
pip工具常用命令: pip命令示例 说明 pip download SomePackage[==version] 下载扩展库的指定版本,不安装 pip freeze [> requiremen ...
- (转)go rabbitmq实践
转载自:http://www.cnblogs.com/shi-meng/p/4800080.html 1:驱动 本来打算自己写一个驱动的,后来发现github上面已经有了,那我就直接拿现成的了, 驱动 ...
- 【转】学习FFmpeg API – 解码视频
ffmpeg是编解码的利器,用了很久,以前看过dranger 的教程,非常精彩,受益颇多,是学习ffmpeg api很好的材料.可惜的是其针对的ffmpeg版本已经比较老了,而ffmpeg的更新又很快 ...