标签:sam html style main hit case second round ensure
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.
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.
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
5 1 2 4 14 9
3
1 3
2 5
4 1
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 }
标签:sam html style main hit case second round ensure
原文地址:https://www.cnblogs.com/zllwxm123/p/11182671.html