Title Description
The task is really simple: given N exits on a high way which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits
Input Format
Each input file contains on test case. For each case, the first line contains an integer N (in[3, 105]), followed by N integer distances D1 D2 ... DN , where D1 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 exists are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107
Output Format
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
The meaning of problems
N nodes in a circle adjacent the known distance between the two points, and can only be moved to an adjacent site. Then given a query M, ask each given two numbers A and B, i.e., the node number (1≤A, B≤N), seeking from A
No. shortest distance from the node to the node B,
Input Format
Each input file contains a test case
In each case, the first row contains an integer N (in [3,10 5 ]) is followed by n integers distance D . 1 D 2 ... D N , wherein D . 1 is the i-th and (i + the distance between 1) the outlet, and D N between the N-th and first exit outlet.
All numbers in a row are separated by spaces. The second line gives a positive integer M (≦ 10 . 4 ), followed by M rows, each row including a pair of exit, the exit condition is numbered from 1 to N. Ensure that the local round-trip distance of no more than 10 7
Input sample, there are three query, are 1,3; 2,5; 4,1
Problem-solving ideas
- This is a circle, the circle of minimum distance between the end points, is the minor arc between the two points
const int MAXN = 100000 ; int DIS [MAXN], A [MAXN]; // DIS array has the meaning described, A [i] to store the number i from the i + 1 vertices int main ( int argc, char * the argv [ ]) { int SUM = 0 , Query, n-, left, right; Scanf ( " % D " , & n-); for ( int I = . 1 ; I <= n-; I ++ ) { Scanf ( " % D " , & A [I]); SUM + = A [I]; DIS [I] = SUM;// initialize dis array } Scanf ( " % D " , & Query); for ( int I = 0 ; I <Query; I ++ ) { Scanf ( " % D% D " , & left, & right); IF (left> right) { the swap (left, right); } int TEMP = DIS [right - . 1 ] - DIS [left - . 1 ]; // opposite end points of the arc clockwise the printf ( " % D \ n- " , min ( TEMP, SUM - TEMP)); // select output minor arc } return 0; }