编程大赛-5
|
Routers
|
We have n routers (enumerated from 0 to n - 1). Each router (except for 0 which is also a gateway) sends all data packets it receives to a certain router all the time. Eventually all packets reach the gateway - router 0.
Input: (standard input)
The first line contains a number of all routers: n, 1 <= n <= 100000.
The second line contains n space separated numbers: i-th number denotes a router to which data packets go when sent by the i-th router (1 <= i <= n - 1).
Each of the next lines contains two space separated numbers: a pair of routers R1 R2 for which you must find out whether a packet sent from R2 will go through R1.
Output: (standard output)
For each R1 R2 pair of routers, your program should print out 1 if a packet sent by the second router passes the first one, 0 otherwise.
Example:
For sample input:
7
0 1 5 0 4 1
1 4
4 3
0 6
3 4
1 0
2 2
a correct output format:
0
1
1
0
0
0
_____________
|
Audit
|
AP system stores messages in a database. Unfortunately, there is a bug in an audit process which runs every midnight. Whenever this poor audit starts, it destroys two messages - the shortest and the longest one which have been stored in the database before the midnight. At least two messages are stored in database whenever audit starts. No more than 1500000 messages are stored in the database at any time. Write a program which calculates how many kilobytes of messages were lost during ndays.
Input: (standard input)
+Line 1 contains a number of days n, 1 ≦ n ≦ 5000.
+Line i + 1, (where 1 <= i <= n), specifies the message sizes for the i-th day. The first number is m, the number of messages stored, with 0 <= m <= 1000. It is followed by m numbers specifying the size of each message in kilobytes as a positive integer not greater than 1000000.
Output: (standard output)
Your program should print out how many kilobytes of messages were lost.
Example:
For sample input:
4
5 5 2 3 4 8
2 9 5
0
4 1 5 7 4
a correct output format:
39
Hint: (8 + 2) + (9 + 3) + (5 + 4) + (7 + 1) = 39
|
The minimum cost
|
The taxicab distance between the point P1 with coordinates (x1, y1) and the point P2 at (x2, y2) is |x1 - x2| + |y1 - y2|. There are n points with integer coordinates. A cost of the point is defined as the sum of taxicab distances to the rest of the points. Write a program to calculate the minimum cost for these points.
Input: (standard input)
+Line 1 contains: n, 1 ≦ n ≦ 100 000.
+Line i + 1, (where 1 ≦ i ≦ n) of standard input describes coordinates of i-th point. It contains two space-separated integers: xi and yi, where 0 ≦ xi , yi ≦ 1000000000.
Output: (standard output)
Your program should print out the minimum cost.
Example:
For sample input:
3
4 6
1 2
3 1
a correct output format:
9

浙公网安备 33010602011771号