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 ...
随机推荐
- windos安装maven
1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...
- python之路--装饰器
二 .通用装饰器的写法 python里面的动态代理. 存在的意义: 在不破坏原有的函数和原有函数的调用基础上,给函数添加新的功能 def wrapper(fn): # fn是目标函数. def inn ...
- C# 中那些常用的工具类(Utility Class)(三)
今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...
- vue.js2.0:如何搭建开发环境及构建项目
1,安装node.js Node.js官网:https://nodejs.org/en/ 进入Node.js官网,选择下载并安装Node.js.安装过程只需要点击“下一步”即可, 如下图,非常简单. ...
- phonegap-plugin-contentsync
一.API 1.ContentSync.sync(options) options.src : 字符串类型 (必选项)远程托管内容的URL.更新一个生产环境下的APP,常使用HTTPS option ...
- How to enable AHCI on Windows7
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\msahci
- react 自我小计
1.react中的方法调用,在onClick事件中不需要加小括号. <button onClick={this.show}>方法的调用</button> show(){ con ...
- 【python练习题】程序8
#题目:输出 9*9 乘法口诀表. for i in range(1,10): k = '' for j in range(1,i+1): k += '%s * %s = %s '%(i,j,i*j) ...
- hdu-4738(tarjan割边)
题意:给你n个点,m条边,边有权值,问你最小的花费使图不连通: 解题思路:就是求边权最小的割边,但这道题有坑点: 1.有重边(桥的两个点有重边时,你去掉一条边并没什么d用): 2.当权值为0的时候,我 ...
- python第三方库的四种安装方法
1,直接pip install安装 2,在python-->default setting-->project interprer-->add 3,在这个链接里找到需要的包,下载 h ...