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. jenkins第一次登陆,输入完密码之后,卡在了SetupWizard[jenkins]处

    问题描述: 前几天在安装测试环境的jenkins,启动tomcat之后,通过页面进行登录,输入完初始化的密码之后,就一直卡在 SetupWizard[jenkins]这个地方. 问题如下图: 备注:等 ...

  2. ros网址链接

    安装教程:http://wiki.ros.org/cn/indigo/Installation robotics:http://www.rethinkrobotics.com/ 学习教程:http:/ ...

  3. fork函数详解--20

     一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同 ...

  4. raw_input()

    raw_input() 用于接收标准输入,并把标准输入当成字符串类型来处理,只能在 Python2 中使用,Python3 中没有这个函数 #!/usr/bin/env python #-*- cod ...

  5. shell、cmd、dos和脚本语言杂谈(转)

    问题一:DOS与windows中cmd区别   在windows系统中,“开始-运行-cmd”可以打开“cmd.exe”,进行命令行操作. 操作系统可以分成核心(kernel)和Shell(外壳)两部 ...

  6. Runtime 运行时之一:消息转发

    解释一 上一篇文章咱们提到了Runtime的消息传递机制,主要围绕三个C语言API来展开进行的.这篇文章我将从另外三个方法来描述Runtime中另一个特性:消息转发机制. 一.消息转发机制 当向某个对 ...

  7. js积累

    动态加载JS文件(function (d) { var t=d.createElement("script");t.type="text/javascript" ...

  8. [置顶] Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  9. ubuntu的两种网络连接模式

    ubuntu的网络连接分成两种类型,一种是modern 模式, 这种模式的配置通过ifconfig命令来进行配置,重启之后失效,这种模式就是在destop 下右上角的网络连接.如图所示 modern模 ...

  10. 在腾讯云服务器上体验Docker

    版权声明:本文由姚俊刚原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/85 来源:腾云阁 https://www.qclou ...