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 ...
随机推荐
- vue小问题库
引入vue组件命名时,不用特殊标签,比如<head>,不然会按特殊标签处理
- C# Note3:大话Ninject
前言 之所以研究Ninject,是因为初入职在开发XX项目的ComponentService部分时用到了它,一下子发现了它的强大.渐渐地发现在项目中,有时会用到优秀的第三方开源库,这些都是前人智慧的结 ...
- redis设置防火墙的问题
Linux 下载安装配置Redis完整步骤 安装: 1.获取redis资源 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 2. ...
- python爬虫之线程池和进程池
一.需求 最近准备爬取某电商网站的数据,先不考虑代理.分布式,先说效率问题(当然你要是请求的太快就会被封掉,亲测,400个请求过去,服务器直接拒绝连接,心碎),步入正题.一般情况下小白的我们第一个想到 ...
- python学习笔记(11)--数据组织的维度
数据的操作周期 存储 -- 表示 -- 操作 一维数据表示 如果数据有序,可以使用列表[]:如果数据没有顺序,可以使用集合{} 一维数组存储 存储方式一:空格分隔 ,使用一个或多个空格分隔进行分隔, ...
- Visual Studio2012调试时无法命中断点
今天在调试代码的时候发现在Debug模式下无法命中断点,然后一步步去检查原因,最后发现是在项目-->属性-->生成-->高级-->调试信息被设置为None,然后在选项中将其选择 ...
- Python:matplotlib绘制条形图
条形图,也称柱状图,看起来像直方图,但完是两码事.条形图根据不同的x值,为每个x指定一个高度y,画一个一定宽度的条形:而直方图是对数据集进行区间划分,为每个区间画条形. 将上面的代码稍微修改一 ...
- PHPWord插件详解
一下载PHPWorld并配置项目 1.PHPWord框架文件如下: 二使用word模板并使用PHPWord生成doc文件 例如:源代码如下: <?php require_once '../PHP ...
- layui弹窗 之 iframe关闭
1)关闭特定iframe //当在iframe页面关闭自身时,在iframe页执行以下js脚本 var index = parent.layer.getFrameIndex(window.name); ...
- How to execute a Stored Procedure with Entity Framework Code First
Recently I worked on a project, which I started as code first and then I forced to switch to Databas ...