首页 技术 正文
技术 2022年11月14日
0 收藏 405 点赞 4,129 浏览 1379 个字

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, 105]), followed by N integer distances D1 D2 … DN, where Di is the distance between the i-th and the (i+1)-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 (<=104), 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 107.

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
这题做这么久真是没治了,本来不是很难的一题,被自己蠢到了……
#include <iostream>
#include <cmath>
using namespace std;int exitPort[]; //开始少一个0,可见细心的重要性, 10^5并不是10005,不是五位数谢谢int main()
{
int N;
cin >> N; exitPort[] = ; int sum = ;
for(int i = ; i <= N + ; i++)
{
int road;
cin >> road;
sum += road;
exitPort[i] = sum;
} int M;
cin >> M;
while(M--)
{
int in, out;
cin >> in >> out; if(sum - abs(exitPort[out] - exitPort[in]) > abs(exitPort[out] - exitPort[in])) //由于写成了abs(sum - exitPort[out] + exitPort[in]) 有一个点一直没过
{
cout << abs(exitPort[out] - exitPort[in]) << endl;
}
else
cout << sum - abs(exitPort[out] - exitPort[in]) << endl;
} return ;
}
这题唯一的收获就是定义数组的时候最好不要用exit做名称,有的OJ来说, exit是内置的变量,可能会有编译错误,因此修改为exitPort
上一篇: sencha touch 总结
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919