1046 Shortest Distance (20 分)

Posted zllwxm123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1046 Shortest Distance (20 分)相关的知识,希望对你有一定的参考价值。

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]), followed by Ninteger distances D?1?? D?2?? D?N??, where D?i?? is the distance between the i-th and the (-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(≤), 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 9
3
1 3
2 5
4 1

Sample Output:

3
10
7

水题,就是一个前缀和就完事。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 int an[100005];
 5 int main()
 6     cin >> n;
 7     int sum = 0;
 8     for(int i = 1; i <= n ; i ++)
 9         cin >> an[i];
10         sum += an[i];
11         an[i] += an[i-1];
12     
13     cin >> m;
14     int xx, yy, x, y;
15     for(int i = 0; i < m; i ++)
16         cin >> xx >> yy;
17         y = max(xx,yy);
18         x = min(xx,yy);
19         int cnt = an[y-1]-an[x-1];
20         int cnt1 = sum - cnt;
21         int ans = min(cnt, cnt1);
22         cout << ans << endl;
23     
24     return 0;
25 

 



以上是关于1046 Shortest Distance (20 分)的主要内容,如果未能解决你的问题,请参考以下文章

1046 Shortest Distance (20 分)(简单模拟,前缀和)

PAT——甲级1046S:shortest Distance

pat 1046 Shortest Distance(20 分) (线段树)

(C++)1046 Shortest Distance

A1046——入门模拟 Shortest Distance

1046 Shortest Distance (20 分)难度: 简单 / 知识点: 前缀和 / 破环成链