Disk ScheduleTime

** limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)**

Total submission (s): 2368 Accepted Submission (s): 333

Problem Description

There is a lot of demand to read data from disk, including sequential reads, random reads. To improve efficiency, you need to manually arrange disk reads. However. In reality, such a practice is very complex.

Let's consider a relatively simple scenario. Disks have many tracks, and each track has many sectors for storing data. When we want to read the data in a specific sector, the head needs to jump to a specific track, the detailed sector for the read operation. For the sake of simplicity, if the head is able to rotate clockwise or counterclockwise at a certain track, the time to rotate the week is units of time. The head can also be arbitrarily moved to a track for reading, each jump to an adjacent track time is 400 unit time, jump before and after the head of the sector position unchanged. The time to read the data is 10 units of time. The sector position of the head before and after reading is unchanged.

The head can only do one thing at the same time: Jump track, rotate or read. Now, it is necessary to read a set of data on disk, if each track has at most one read request, this read sector is a track on the distribution of 0 to 359 of an integral point sector, that is, a sub-division of the track.

The starting point of the head is in the 0 track 0 sector. There is no data read at this time.

After all reading, the head needs to return to the starting point of the 0 Track 0 sector. Please complete the minimum time required for a given read.

Input

The first line of input includes an integer M (0<m<=100) that represents the number of groups of test data.

For each set of test data, the first line consists of an integer N (0<n<=1000) representing the number of data to be read. Each line then consists of two integers T and S (0<t<=1000,0<= s<). Indicates the track and sector of each data, and the tracks are arranged in ascending order and are not repeated.

Output

For each set of test data. Outputs an integer. Indicates the time that is required to complete all reads.

Sample Input

31 1 10 31 20 3 305 102 1 102 11

Sample Output

830 4090 1642 The problem: taking the Euclidean travel quotient issue simply replace the distance between the two points in the topic (distance = two points of the track difference *400+ two points). It is important to note that the sector difference is the length of the orbital small arc: i.e. smaller than ABS (A[I].Y-A[J].Y). The distance between sector 350 and sector 10 is 20 instead of 340. Another point is to add the starting point (0,0). The following are the ideas of the Euclid Travel Quotient of the god of arrogance; link http://blog.csdn.net/weyuli/article/details/19752217 in fact the so-called Euclid travel Quotient problem is

**from 1 to N and then in the shortest path from N to 1, the order of the points to go through must be small to large, and the order of the points passing through must be from large to small, and each point can only pass once (1 and N does not count), output the shortest length.**

Thinking "Turn": Euclid's Travel quotient problem is the problem of determining the shortest closed journey of a connected point on a given n point on a plane. (a) a solution to the 7 point problem is given.

The general form of the problem is NP-complete, so the solution needs more time than the polynomial.

J.L Bentley recommends simplifying the problem by simply considering a two-tone journey (Bitonic Tour), which starts at the left-most point. Strictly from left to right until the right point, and then strictly from right to left until the starting point.

(b) The shortest two-tone route showing the same 7 points.

In such a case, the algorithm of the polynomial is possible. In fact. There is an algorithm for determining the O (n*n) time of the optimal two-tone route.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdi3mdqwmw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" Figure A "width=" "height=" > " **a** " c1> Figure B

Note: Seven points on a flat surface that are displayed on a single unit grid. A) The shortest closed route, which is approximately 24.89 in length. This route is not double-tuned. b) The shortest two-tone closed route on the same set of points. The length is approximately 25.58.

This is a calculated study questions on the 15-1.

First, the points to be given are sorted, KEYWORDX. Numbering again. From left to right 1, 2. 3, ..... N.

Define D[I][J], which represents the distance between node I and Node J.

Define DP[I][J]. Indicates from I to 1, and then from 1 to J. (Note that the i>j. and not connected. ）

For a random point I. There are two ways to connect, one is (a) to see, I is connected to the i-1, and the other is (b). I is not connected to i-1.

Based on the two-tone journey. We know that the node n must be connected to N, so suppose we ask for the dp[n][n-1], just add it d[n-1][n] is the shortest two-tone closed route.

Basis. Very easy to write equations:

DP[I][J]=DP[I-1][J]+D[I][I-1];

Dp[i][i-1]=min (Dp[i][i-1],dp[i-1][j]+d[j][i]);

Here is the code implementation:

#include <cstdio> #include <cstring> #include <cmath>using namespace std;const int maxn = 1010;int dp[ Maxn][maxn];int d[maxn][maxn];struct point{int x, y;} A[maxn];int Dis (int i, int j)//calculates the distance between two points {int p,q; P=abs (A[I].Y-A[J].Y) >q?

Q:abs (A[I].Y-A[J].Y); Find the length of the small Arc return (ABS (a[i].x-a[j].x) *400+p); Distance = Two points of the track difference *400+ two points of the sector difference}int main () {int t,n; scanf ("%d", &t); while (t--) {scanf ("%d", &n); a[1].x=0; a[1].y=0; Add the starting point (0,0) plus for (int i = 2; I <= n+1; i++) scanf ("%d%d", &a[i].x, &A[I].Y); for (int i = 1, i <= n+1; i++) {for (int j = 1; J <= N+1; j + +) {D[i][j] = Dis (i, j); D[I][J] is the distance from the I point to the J Point}} dp[1][2] = d[1][2]; for (int i = 3, I <= n+1; i++) {for (int j = 1; j < I-1; J + +) {Dp[j][i] = Dp[j][i-1] + d[i-1][i]; /* Dp[j][i] for J point to 1, then from 1 to I point, this step is to prepare for the next cycle of dp[i][i+1]. The fact is that figure A */} Dp[i-1][i] = 999999999; for (int j = 1; j < I-1; J + +) { int sum = dp[j][i-1] + d[j][i]; if (Dp[i-1][i] > Sum) dp[i-1][i] = sum; /* Dp[i-1][i] for the i-1 point to 1 points, and then from 1 to I point of the shortest distance, this distance only add edge D[i-1][i] is from 1 point to I point of the shortest closed journey, in fact, is the figure b * *}} DP[N+1][N+1] = dp[n][n+1] + d[n][n+1]; printf ("%d\n", dp[n+1][n+1]+10*n); /* Dp[n+1][n+1] is finally the shortest closed journey, n+1 point to 1 points. The shortest distance from 1 to n+1 points, 10*n is the time of the data in the reading point */} return 0;}

Baidu Star Qualifying race--disk Schedule (double travel quotient issue)