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,10​5​​]), followed by N integer distances D​1​​ D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (i+1)-st exits, and D​N​​is 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 (≤10​4​​), 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 10​7​​.

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:

  1. 5 1 2 4 14 9
  2. 3
  3. 1 3
  4. 2 5
  5. 4 1

Sample Output:

  1. 3
  2. 10
  3. 7

题目大意:给出一个圈,并且每个点之间的距离给出了;输出查询每对点之间的最短距离。

//感觉如果直接去查,肯定会超时的。。 想使用dp的思想,但写了一下状态转移公式,发现之前的并不能依次计算。感觉就是无厘头的循环啊。不太会这样的题目。

//我的想法就是将from设置为小的那个,先计算from-to的最短距离,这个遍历一下就可以;再计算0-from+to-n的距离这样。

代码来自:https://www.liuchuo.net/archives/2021

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main() {
  5. int n;
  6. scanf("%d", &n);
  7. vector<int> dis(n + );
  8. int sum = , left, right, cnt;
  9. for(int i = ; i <= n; i++) {
  10. int temp;
  11. scanf("%d", &temp);
  12. sum += temp;
  13. dis[i] = sum;
  14. }
  15. scanf("%d", &cnt);
  16. for(int i = ; i < cnt; i++) {
  17. scanf("%d %d", &left, &right);
  18. if(left > right)
  19. swap(left, right);//可以直接使用swap函数。
  20. int temp = dis[right - ] - dis[left - ];
  21. printf("%d\n", min(temp, sum - temp));
  22. }
  23. return ;
  24. }

//这个代码简直是非常神奇了,我是想不出来。

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[环形][比较]的更多相关文章

  1. PAT 1046 Shortest Distance

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  2. pat 1046 Shortest Distance(20 分) (线段树)

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  3. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  4. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  5. 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 ...

  6. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  7. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  8. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  9. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

随机推荐

  1. [java] jstack 查看死锁问题

    package com.xwolf.java.thread.ch2; /** * Created by Administrator on 2016/1/4 0004. */ public class ...

  2. NHibernate初学一之简单增删改查

    以前有简单了解NHibernate,但因项目一直没有运用所以也没有机会进行一个详细了解:最近利用一点空闲的时间认真学习它:同样希望把学习过程进行简单记录,今天简单写一个针对MSSQL数据库的增删改查的 ...

  3. 超全面的JavaWeb笔记day10<Response&Request&路径&编码>

    1.Response 2.Request 3.路径 4.编码 请求响应流程图 response 1.response概述 response是Servlet.service方法的一个参数,类型为java ...

  4. Java类的设计----关键字super

    关键字super 在Java类中使用super来引用父类的成分 super可用于访问父类中定义的属性 super可用于调用父类中定义的成员方法 super可用于在子类构造方法中调用父类的构造方法 su ...

  5. GIS-005-Dojo & jQuery 事件处理

    1.添加事件 dojo.connect(dojo.byId("SelectRect"), "onclick", function () { //todo }); ...

  6. iOS 数据类型转换

    1.NSString转化为UNICODE String:(NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname c ...

  7. Oracle分页查询sql语句

    1. select * from ( select  t.*, rownum RN from TABLE_NAME  t ) where RN > 0 and RN <= 15 2. se ...

  8. Cloud Foundry技术全貌及核心组件分析

    原文链接:http://www.programmer.com.cn/14472/ 历经一年多的发展,Cloud Foundry的架构设计和实现有了众多改进和优化.为了便于大家了解和深入研究首个开源Pa ...

  9. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  10. Common Subsequence 最大公共子序列问题

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...