博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1046 Shortest Distance
阅读量:4541 次
发布时间:2019-06-08

本文共 1656 字,大约阅读时间需要 5 分钟。

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]), followed by N integer distances D1​​ D2​​ ⋯ DN​​, where Di​​ is the distance between the i-th and the (-st exits, and DN​​ 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 (≤), 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 1.

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 931 32 54 1

Sample Output:

3107
1 #include 
2 #include
3 using namespace std; 4 int N, M; 5 int main() 6 { 7 cin >> N; 8 int num, a, b; 9 vector
sum(N + 1, 0);10 for (int i = 1; i <= N; ++i)11 {12 cin >> num;13 if (i == N)14 sum[0] = sum[N] + num;15 else16 sum[i + 1] = sum[i] + num;17 }18 cin >> M;19 for (int i = 0; i < M; ++i)20 {21 cin >> a >> b;22 if (a > b)23 swap(a, b);24 int d1 = sum[b] - sum[a];25 int d2 = sum[0] - sum[b] + sum[a] - sum[1];26 cout << (d1 < d2 ? d1 : d2) << endl;27 }28 return 0;29 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11273255.html

你可能感兴趣的文章
Java之路——Java初接触
查看>>
2018.12.27学习JavaScript
查看>>
理工之 A+B Problem III
查看>>
软件工程第一次作业
查看>>
【Android 界面效果24】Intent和PendingIntent的区别
查看>>
node学习之搭建服务器并加装静态资源
查看>>
android 按menu键解锁功能的开关
查看>>
Linux 下的dd命令使用详解
查看>>
POJ-1273 Drainage Ditches 最大流Dinic
查看>>
ASP.NET学习记录点滴
查看>>
[Noip2016] 愤怒的小鸟
查看>>
JAVA wait()和notifyAll()实现线程间通讯
查看>>
python全栈脱产第11天------装饰器
查看>>
[总结]数据结构(板子)
查看>>
C# 笔记
查看>>
[转]人人店短信插件开发
查看>>
[转]c# System.IO.Ports SerialPort Class
查看>>
14. 最长公共前缀
查看>>
Redis文档
查看>>
项目重构
查看>>