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. How to Setup Cordova for Windows 7

    Setup Cordova Text Editor / IDE You may need to prepare an IDE or Editor for working. Here for examp ...

  2. Oracle Apex 有用笔记系列 2 - 文件上传管理

    1. 页面设计 页面A有若干region, 当中一个region用于文件列表管理(包含显示,下载.删除).如图A. 在页面A有一button,点击它会调用页面B,页面B负责文件上传.如图B. 图A 图 ...

  3. Java课后简答题

    1.简述Java的特点. 面向对象.跨平台性.健壮性.安全性.可移植性.多线程性.动态性等. 2.简述JRE与JDK的区别. JRE(Java Runtime Environment,Java运行时环 ...

  4. Intel S5000VSA(SAS)主板设置RAID 步骤【转】

    Intel S5000VSA(SAS)主板设置RAID 步骤 我近日亲自安 装了一台服务器,用的是intel S5000VSA 4DIMM主板,因为在安装过程中没有注意到一些细节,所以在安装时碰到了一 ...

  5. oracle数据库sql比较日期

    select * from cc_random_check_info t where check_time > to_date('2016-09-09','yyyy-MM--dd')

  6. poj_3461 kmp

    题目大意 给定两个字符串S1, S2,求S1中包含多少个S2串.其中S1长度最大为 1000000, S2长度最大为10000. 题目分析 典型的字符串匹配问题,直接匹配显然会超时,考虑使用kmp算法 ...

  7. mysql使用笔记(一)

    一.安装 使用免安装的版本进行安装: 1. 解压到安装目录 2. 拷贝目录下的 my-default.ini 文件为 my.ini 文件 3. 修改my.ini 文件内容为 [client] port ...

  8. Splay模板 1.0

    struct Splay{ int rt,sz; ///根节点,树节点总数 ],fa[N];///值,左右儿子,父亲 void spin(int t){ ///旋转操作 ]==t; son[x][y] ...

  9. 2800 送外卖[状态压缩dp]

    2800 送外卖  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 有一个送外卖的,他手上有n份订单,他 ...

  10. IT公司常见的内网漏洞表格

    访问控制类漏洞与隐患 这一类漏洞与隐患属于访问控制与身份鉴别问题,一般有没有配置访问控制.访问控制弱(弱口令或者空口令),身份鉴别可以绕过等问题 漏洞协议组件 漏洞类型 漏洞评级 SSH 弱口令 严重 ...