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 ...
随机推荐
- [java] jstack 查看死锁问题
package com.xwolf.java.thread.ch2; /** * Created by Administrator on 2016/1/4 0004. */ public class ...
- NHibernate初学一之简单增删改查
以前有简单了解NHibernate,但因项目一直没有运用所以也没有机会进行一个详细了解:最近利用一点空闲的时间认真学习它:同样希望把学习过程进行简单记录,今天简单写一个针对MSSQL数据库的增删改查的 ...
- 超全面的JavaWeb笔记day10<Response&Request&路径&编码>
1.Response 2.Request 3.路径 4.编码 请求响应流程图 response 1.response概述 response是Servlet.service方法的一个参数,类型为java ...
- Java类的设计----关键字super
关键字super 在Java类中使用super来引用父类的成分 super可用于访问父类中定义的属性 super可用于调用父类中定义的成员方法 super可用于在子类构造方法中调用父类的构造方法 su ...
- GIS-005-Dojo & jQuery 事件处理
1.添加事件 dojo.connect(dojo.byId("SelectRect"), "onclick", function () { //todo }); ...
- iOS 数据类型转换
1.NSString转化为UNICODE String:(NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname c ...
- Oracle分页查询sql语句
1. select * from ( select t.*, rownum RN from TABLE_NAME t ) where RN > 0 and RN <= 15 2. se ...
- Cloud Foundry技术全貌及核心组件分析
原文链接:http://www.programmer.com.cn/14472/ 历经一年多的发展,Cloud Foundry的架构设计和实现有了众多改进和优化.为了便于大家了解和深入研究首个开源Pa ...
- 并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submiss ...
- Common Subsequence 最大公共子序列问题
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...