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 ...
随机推荐
- Android下NDK开发环境搭建
Android下NDK开发环境搭建 1. AndroidNDK安装与配置 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP部 ...
- ORM对象关系映射之使用GreenDAO进行CRUD操作
在Android中,我们都知道使用的数据库是SQLite,而使用这种原生的数据库非常繁琐,它对表的管理和进行CRUD操作都需要我们写sql语句,在进行多表关联的操作上,更是需要写一堆sql,而且维护起 ...
- Android控件属性android:visibility的invisible与gone的区别
"invisible" : 不可见 "gone" : 隐 藏 主要区别在于控件设置了invisible后控件不可见,但是保留了控件在界面上的空间, ...
- Leetcode_231_Power of Two
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334243 Given an integer, writ ...
- objective-c 2.0的字面量Literals
obj-c 2.0增加了许多核心对象字面量的简单语法,向ruby学习吗? 直接上代码: #import <Foundation/Foundation.h> int main(void){ ...
- Android 客户端与服务器交互
在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...
- LeetCode(36)- Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- ruby创建某些“关键字”方法别名的语法
begin和end是ruby的关键字,但是Range中也有名称为begin和end的实例方法.现在问题来了:怎么创建它们的别名方法? 如果用class Range;alias begin_x begi ...
- python 3下基于select模型的事件驱动机制程序
它的基本原理就是select/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程.它的流程如图: 当用户进程调用了select,那么整个 ...
- Course1-Python数据类型总结
一. 开始正式学习前的tips: Python和其他语言有很多类似, 也有一些差异, 下面先总结了一些基本语法上的注意事项 1. 注意缩进 2. 一行语句分为多行显示: \ 3. 注释: 单行注释#, ...