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:

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

  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. 【观点】“马云:金融是要为外行人服务",这个观点其实并不新鲜

    不久前,马云在外滩国际金融峰会演讲,称金融行业需要“搅局者”.他说:“今天的金融,确实做得不错,没有今天这样的金融机构,中国的经济30年来不可能发展到今天,但是靠今天银行的机制,我不相信能支撑30年以 ...

  2. 判断字符串是否为json字符串

    public static class JsonSplitExtention { public static bool IsJson(this string json) { return JsonSp ...

  3. Linux环境PHP5.5以上连接SqlServer2008

    linux版本:64位CentOS 6.4 Nginx版本:nginx1.8.0 php版本:php5.5.28 Sqlserver版本:2008 FreeTDS版本:0.95 关于Linux环境安装 ...

  4. hadoop程序MapReduce之average

    需求:求多门课程的平均值. 样板:math.txt zhangsan 90 lisi 88 wanghua 80 china.txt zhangsan 80lisi 90wanghua 88 输出:z ...

  5. Java创建数组的三种方法

    ■ 第一种: int[] arr=new int[6]; arr[0] = 1; arr[1] = 2 arr[2] = 3; arr[3] = 4; arr[4] = 5; arr[5] = 6; ...

  6. 上传控件CSS用图片代替

    <style type="text/css"> a.btn {width: 120px;height: 42px;overflow: hidden;display: b ...

  7. 【PHP】数字补零的两种方法

    在php中有两个函数,能够实现数字补零, str_pad() sprintf() 函数1 : str_pad 顾名思义这个函数是针对字符串来说的这个可以对指定的字符串填补任何其它的字符串 例如:str ...

  8. Weui upLoader

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现

    MQTT moquette 的Server发布主题 package com.etrip.mqtt.future; import java.net.URISyntaxException; import  ...

  10. jquery类似方法的比较(一)

    1. $(div + p) & $(div ~ p) & $(div p) & $(div > p) $(div + p)选择紧挨在div后面的P元素 $(div ~ p ...