2014 Baidu star question 1002-disk schedule

Source: Internet
Author: User
Tags benchmark

Problem description
There are many requirements for reading data from the disk, including sequential reading and random reading. To improve efficiency, You need to manually read the disk. However, in reality, such an approach is very complicated. We consider a relatively simple scenario. A disk has many tracks, and each track has many sectors for data storage. When we want to read data in a specific sector, the head must jump to a specific track and detailed sector for read operations. For simplicity, if the head can rotate clockwise or counterclockwise at a constant speed in a certain track, the cycle of rotation is 360 units of time. The head can also be freely moved to a certain track for reading. The time for each jump to an adjacent track is 400 units of time, and the position of the front and back sides of the head remains unchanged. The time for reading data at a time is 10 units, and the position of the sector of the front and back heads remains unchanged. At the same time, the head can only do one thing: Jump to the track, rotate or read. Today, a set of data needs to be read from the disk. If each track has at most one read request, the read sector is an integer sector distributed between 0 and 359 on the track, that is, a certain 360-level sub-point of the track. The starting point of the head is 0-track and 0-sector. No data is read at this time. After all the data is read, the head must return to the starting point of the 0-sector on the 0-track. The minimum time required to complete the given read operation.
 
Input
The first line of the input contains an integer m (0 <m <= 100), indicating the number of groups of the trial data. For each group of benchmark data, the first row contains an integer N (0 <n <= 1000), indicating the number of data to be read. The next row contains two integers T and S (0 <t <= 360, 0 <= S <), indicating the magnetic channels and sectors of each data. The magnetic channels are arranged in ascending order, and there is no repetition.
 
Output
An integer is output for each group of benchmark data, indicating the time required to complete all the reads.
 
Sample Input
311 1031 203 305 1021 102 11
 
Sample output
83040901642

This is the most difficult topic in this case. As the number of people who pass the test is the minimum, people constantly feel that their programs are correct and cannot pass the test.

A lot of people have used simple violence to solve this problem. This is a wrong method, of course, WA.

The correct solution should be bitonic TSP-able to take an exam in Chapter 15th of <Introduction to algorithm>.

Because the question limits every data to be asked once, it is necessary to start from the starting point, and finally return to the starting point, forming a perfect match with the problem of bitonic TSP.



For example, Figure B shows the route of the bitonic TSP. A is the optimal route, but the problem here limits the optimal route of the TSP.

Divergent thinking:

1. Instead of simulating data reading, you can read data from the beginning and then read it back. Instead, you can imagine that there are two Data Reading headers that keep reading the end point and the two heads overlap.

2. The time for reading data is calculated as follows: N x 10 (n data records, each of which requires 10 units of time.


#include <stdio.h>#include <cmath>#include <stdlib.h>#include <vector>using namespace std;class DiskSchedule_3{struct TS{int T, S;};int getDist(vector<TS> &gra, int i, int k){int d = (gra[k].T-gra[i].T) * 400;if (abs(gra[k].S-gra[i].S) <= 180) d += abs(gra[k].S-gra[i].S);else d += 360 - abs(gra[k].S-gra[i].S);return d;}public:DiskSchedule_3(){int M, N;scanf("%d", &M);while (M--){scanf("%d", &N);vector<TS> gra(N+1);for (int i = 1; i <= N; i++){scanf("%d %d", &gra[i].T, &gra[i].S);}vector<vector<int> > tbl(N+1, vector<int>(N+1));for (int i = 1; i <= N; i++){for (int k = 0; k <= i; k++){if (k+1 < i) tbl[k][i] = tbl[k][i-1] + getDist(gra, i-1, i);else{tbl[k][i] = tbl[0][k] + getDist(gra, 0, i);for (int r = 1; r < i; r++){int d = tbl[r][k] + getDist(gra, r, i);tbl[k][i] = min(tbl[k][i], d);}}}}printf("%d\n", tbl[N][N] + 10 * N);}}};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.