A1046. Shortest Distance
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 DNis 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
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int N, M, sum = ;
vector<int> dst2src, re;
scanf("%d", &N);
int temp = ;
dst2src.push_back(temp);
for(int i = ; i < N; i++){
scanf("%d", &temp);
sum += temp;
dst2src.push_back(sum);
}
scanf("%d", &M);
for(int i = ; i < M; i++){
int a, b, L1, L2;
scanf("%d%d", &a, &b);
if(a > b) swap(a, b);
L1 = dst2src[b - ] - dst2src[a - ];
L2 = sum - L1;
printf("%d\n", min(L1, L2));
}
return ;
}
总结:1、可使用swap、min函数,引用algorithm即可。
2、预处理:模拟题一般要考虑到时间复杂度,本题中有N个节点。若不做预处理,M组查询,会有M*N的复杂度。若在读入数据的时候,就计算出源点到该点的距离,则在查询时,a到b的距离可用a到源减去b到源,不用遍历。
3、由于是一个环,a到b的距离只有顺逆时针两种可能。
A1046. Shortest Distance的更多相关文章
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- PAT甲级——A1046 Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- A1046. Shortest Distance(20)
17/20,部分超时. #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- [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 ...
随机推荐
- idea创建maven项目的一点关键
maven中的一些概念: POM:项目对象模型(Project Object Model),是项目的一些关键元信息的集合.主要包含项目管理信息.具体的项目描述.开发小组的构 成.源代码库(如CVS)和 ...
- powerdesigner 16.5 不允许有扩展属性,或对象不存在
创建完之后这边会出现 选择刚创建的用户 这样就可以了
- sql left join多表
表A---------------------------------关联第一张表B-----------------------关联第二张表c select * fomr 表名A left join ...
- canvas图形绘制
前面的话 前面分别介绍了canvas的基础用法和进阶用法,本文将使用canvas的各种语法进行图形绘制 绘制线条 [绘制线条] 下面来尝试绘制一段线条 <canvas id="draw ...
- HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别
HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...
- JAVA优先级队列元素输出顺序测试
package code.test; import java.util.Comparator; import java.util.Iterator; import java.util.Priority ...
- fastjson的JSONArray和JSONObject
转自: http://blog.csdn.net/tangerr/article/details/76217924 Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理 ...
- BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆
题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13 根据 ...
- Vue 快速入门
Vue框架介绍 中文文档: https://cn.vuejs.org/v2/guide/ Vue是一个构建数据驱动的web界面的渐进式框架. 目标是通过尽可能简单的API实现响应式的数据绑定和组合的视 ...
- python学习日记(格式化输出,初始编码,运算符)
格式化输出 顾名思义,按照个人意愿定制想输出的格式. name = input('请输入姓名:') age = int(input('请输入年龄:')) job = input('请输入工作:') h ...