1046. Shortest Distance (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1046. Shortest Distance (20)

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  2. [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 ...

  3. [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 ...

  4. maximum shortest distance

    maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  5. [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 ...

  6. 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 ...

  7. [LeetCode] Shortest Distance to a Character 到字符的最短距离

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  8. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  9. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

随机推荐

  1. Leetcode_145_Binary Tree Postorder Traversal

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876769 Given a binary tree, r ...

  2. /etc/fstab文件分析(第二版)

    /etc/fstab文件分析 前面说新建的分区在系统重启之后就会失效,要想永久生效,需要将其写入/etc/fstab文件 一./etc/fstab文件格式:6个组成部分 物理分区名/卷标 挂载点 文件 ...

  3. Unity 数据Json格式的转换

    把对象转换为字节序列的过程称为对象的序列化. 把字节序列化恢复为对象过程称为对象的反序列化. JSON格式的转换,是一大神给我说的,让我拿来存储数据库时对一些数据的处理,感觉特别好用.但是我并没有深入 ...

  4. Linux - 主机的细部权限规划:ACL 的使用

    ACL 是 Access Control List 的缩写,主要的目的是在提供传统的 owner,group,others 的 read,write,execute 权限之外的细部权限配置.ACL 可 ...

  5. myBatis源码学习之SqlSessionFactory

    上一篇博客 SqlSessionFactoryBuilder 中介绍了它的作用就是获得DefaultSqlSessionFactory SqlSessionFactory是一个接口,其具体实现类是De ...

  6. 猴子吃桃问题---C实现

    原题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了了一个.第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每一天早上都吃前一天剩下桃子的一半零一个.到第十天早上想再吃时,发现 只剩下 ...

  7. linux内核自旋锁API

    我们大概都了解,锁这种机制其实是为了保护临界区代码的,关于使用和定义,我总结的API如下: #include <linux/spinlock.h> 定义自旋锁 spinlock_t loc ...

  8. HttpWebRequest类库注意事项以及常见问题

    ServicePoint: 提供HTTP连接的连接管理ServicePointManager: 管理ServicePoint 对象的集合 注意事项:1. 将http的request的keepAlive ...

  9. python snownlp情感分析简易demo

    SnowNLP是国人开发的python类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和T ...

  10. VueJs(5)---V-bind指令

    V-bind指令 一.概述 v-bind  主要用于属性绑定,比方你的class属性,style属性,value属性,href属性等等,只要是属性,就可以用v-bind指令进行绑定. 示例: < ...