2014 Baidu star qualifying round-disk schedule

Source: Internet
Author: User
2014 Baidu star qualifying round-disk schedule
Problem description has a lot of 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.
 
The first line of 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 outputs an integer for each group of benchmark data, indicating the time required to complete all reads.
Sample Input
311 1031 203 305 1021 102 11
 
Sample output
83040901642
 
Source 2014 Baidu STAR Program Design Competition-qualifying
AC code: the most difficult question in this competition, I have been thinking about an optimal greedy strategy, and the results seem to be ineffective. I searched the internet and found it a dynamic planning problem;

Dual-tuning Euclidean Traveling Salesman Problem is a classic dynamic planning problem. Introduction to algorithms (second edition): Question 15-1 and Peking University oj2677.

Description of the traveling salesman problem: N points on the plane determine the shortest closed journey connecting each point. This solution is generally in the form of Np (which can be obtained within the polynomial time)

J. l. bentley recommends that you simply consider bitonictour to simplify the problem. This journey starts from the leftmost point, strictly from left to right to the rightmost point, then strictly from right to left to start point. (B) shows the shortest double-adjustment routes for the same seven points. In this case, polynomial algorithms are possible. In fact, there is an algorithm for determining the optimal dual-tuning route O (N * n) time.

In, A is the shortest closed route, which is not a double adjustment. B is the shortest double-adjustment closed route.


Solution Process:

(1) first, the points are arranged in ascending order of X coordinates, and the time complexity is O (nlgn ).

(2) Search for sub-structure: the path from Pi to PJ is defined as starting from Pi, starting from right to left to P1, and then from left to right to PJ. In this path, all vertices from P1 to Pmax (I, j) are passed only once.

In the definition d (I, j) is the shortest path that meets this condition. We only consider I> = J.

At the same time, dist (I, j) is defined as the linear distance between point pi and PJ.

(3) Optimal Solution: we must require D (N, N ).

There are three solutions to the subproblem d (I, j:

A. When j <I-1, d (I, j) = D (I-1, j) + dist (I-1, I ).

According to the definition, point pi-1 must be on the path pi-PJ, And because j <I-1, the adjacent point on the left of the PI must be pi-1. therefore, the above equations can be obtained.

B. When J = I-1, the point adjacent to the left of PI may be from P1 to pi-1. Therefore, the minimum path must be obtained recursively:

D (I, j) = d (I, I-1) = min {d (K, j) + dist (I, K)}, where 1 <= k <= J.

C. When J = I, the last two points connected on the path may be P1-Pi, P2-Pi... Pi-1-Pi.

Therefore:

D (I, I) = min {d (I, 1) + dist (1, I ),..., d (I, I-1), dist (I-1, I )}..

The above content is from the blog post: http://blog.csdn.net/ljd4245/article/details/26099787.
#include<cstdio>#include<cmath>#include<algorithm>#define INF 100*1000*800#define MAX 1005using namespace std;typedef struct Position{int t;int s;}Position;Position p[MAX];int dp[MAX][MAX];int Distance(int i,int j)//get the distance of track i and track j{int t=(int)fabs(p[i].t-p[j].t)*400;int s1,s2;if(p[i].s<p[j].s){s1=p[i].s;s2=p[j].s;}else{s1=p[j].s;s2=p[i].s;}int l=s2-s1;int r=360-s2+s1;return (l<r?l:r)+t;}int solve(int n){int ans=INF;int i,j;int dis;dp[2][1]=Distance(2,1);for(i=2;i<=n;i++){for(j=1;j<i;j++){dp[i][j]=min(dp[i][j],dp[i-1][j]+Distance(i-1,i));dp[i][i-1]=min(dp[i][i-1],dp[i-1][j]+Distance(j,i));}}for(i=1;i<n;i++){dis=Distance(i,n);if(ans>dp[n][i]+dis)ans=dp[n][i]+dis;}return ans;}int main(int argc,char *argv[]){int n,m;scanf("%d",&m);while(m--){scanf("%d",&n);p[1].t=0;p[1].s=0;for(int i=2;i<=n+1;i++)scanf("%d%d",&p[i].t,&p[i].s);for(int i=1;i<=n+1;i++)for(int j=1;j<=n+1;j++)dp[i][j]=INF;printf("%d\n",solve(n+1)+n*10);}return 0;}

2014 Baidu star qualifying round-disk schedule

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.