PAT 1046 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 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
题目大意:给出一个圈,并且每个点之间的距离给出了;输出查询每对点之间的最短距离。
//感觉如果直接去查,肯定会超时的。。 想使用dp的思想,但写了一下状态转移公式,发现之前的并不能依次计算。感觉就是无厘头的循环啊。不太会这样的题目。
//我的想法就是将from设置为小的那个,先计算from-to的最短距离,这个遍历一下就可以;再计算0-from+to-n的距离这样。
代码来自:https://www.liuchuo.net/archives/2021
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> dis(n + );
int sum = , left, right, cnt;
for(int i = ; i <= n; i++) {
int temp;
scanf("%d", &temp);
sum += temp;
dis[i] = sum;
}
scanf("%d", &cnt);
for(int i = ; i < cnt; i++) {
scanf("%d %d", &left, &right);
if(left > right)
swap(left, right);//可以直接使用swap函数。
int temp = dis[right - ] - dis[left - ];
printf("%d\n", min(temp, sum - temp));
}
return ;
}
//这个代码简直是非常神奇了,我是想不出来。
1.首先sum存储的是环的总长度,
2.这个dis十分有趣了,对于样例有5个点来说:
dis[0]->0
dis[1]-> 1到2的距离,
dis[2]-> 1到3的距离,
dis[3]-> 1到4的距离,
dis[4]-> 1到5的距离,
dis[5]-> 1转一圈后到1的距离。
那么对于left到rigth来说,就是1到他们两者的距离相减,那么另一个距离因为是环,所以就是sum-这个距离。
//学习了!
PAT 1046 Shortest Distance[环形][比较]的更多相关文章
- PAT 1046 Shortest Distance
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- pat 1046 Shortest Distance(20 分) (线段树)
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
随机推荐
- shell中判断一个变量是否为0或者为某个具体的值
需求说明: 在实际写脚本的过程中,需要判断某个变量的值是否为某个数字, 比如,判断某个进程的数量是否为0用来确定进程是否存在,这样的情况. 简单来说,算术比较. 测试过程: 通过以下的脚本来判断mys ...
- module、applet
Each Module is developed as a standalone Windows DLL.Each module can contain one or more applets, an ...
- mysql的引擎myisam和innodb的区别
1. MYISAM和INNODB的不同?答:主要有以下几点区别: a)构造上的区别 MyISAM在磁盘上存储成三个文件,其中.frm文件存储表定义:.MYD (MYData)为数据文件:. ...
- MySQL性能优化(六)-- using filesort,in和exists,慢查询,mysqldumpslow
一.order by产生using filesort详解 1.首先建表和索引(以下使用的sql版本是5.5.54) /*课程表*/ create table course( id int primar ...
- 开源免费天气预报接口API以及全国所有地区代码[值得收藏]
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- 一个php日志类
<?php //author:lixiuran class Log { public static function writeLog($string) { $string = date('H: ...
- 进程防结束之PS_CROSS_THREAD_FLAGS_SYSTEM
有人投到黑防去了,不过黑防不厚道,竟然没给完整的代码,自己整理一份备用吧,驱网.DebugMan.邪八的那群人直接飘过吧. 这种方法的关键在于给线程的ETHREAD.CrossThreadFlags设 ...
- JS-利用ajax获取json数据,并传入页面生成动态tab
封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...
- LeetCode——Pascal's Triangle II
Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...
- Hibernate(1)——数据访问层的架构模式<转>
数据库的概念.逻辑.数据模型概念 应用程序的分层体系结构发展 MVC设计模式与四层结构的对应关系 持久层的设计目标 数据映射器架构模式 JDBC的缺点 Hibernate简介 迅速使用Hibernat ...