Problem Statement
    
When files are stored on a hard disk, they often become fragmented. This means that the file is not stored in sequential sectors on the disk. The first half of a file might be stored in sector 243, while the second half of a file might be far away in sector 105. The goal of defragmenting a hard drive is to arrange the files so that each file is stored in order on sequential sectors of the disk. Thus, if a file required 4 sectors of storage space, it would end up in sectors N, N+1, N+2, and N+3, for some N. Typically, this is done to increase the overall speed of the computer.  There are a number of programs that will defrag a hard disk as described above. However, many of them are painfully slow. You are trying to develop a new algorithm to defrag hard drives, but before you start, you would like to determine how fast you can defrag a very small drive without very many files on it. You will be given the locations of a number of files on a small hard disk, and are to determine the minimum number of sectors that must be moved before the entire drive is defragged. You have enough memory to hold two sectors worth of data at once, but that is all.  You will be given a String[], disk, each of whose elements represents a single file. Each element of disk will be formatted as a single-space delimited list of integers which represent the locations of the parts of the file, in order. Hence, the String, "4 9 6 59 41" represents a file stored in 5 sectors where the first part of the file is in sector 4 of the disk. One way to defrag this file would be to move the contents of sector 9 to sector 5, the contents of sector 59 to sector 7, and the contents of sector 41 to sector 8. By doing this, the file would be stored sequentially in sectors 4-8. You will also be given an int, size, representing the total number of sectors on the disk (sectors 0 through size-1, inclusive, may contain data). You are to return the smallest number of sectors that must be moved to defragment the whole disk. Keep in mind that you can not move data to a sector until any data being stored there is moved.
Definition
    
Class:
DiskDefrag
Method:
minMoves
Parameters:
String[], int
Returns:
int
Method signature:
int minMoves(String[] disk, int size)
(be sure your method is public)
    Constraints
-
size will be between 10 and 100, inclusive.
-
disk will contain between 1 and 12 elements, inclusive.
-
Each element of disk will contain between 1 and 50 characters, inclusive.
-
Each element of disk will be a single-space delimited list of integers, without extraneous leading zeros.
-
Each integer in disk will be between 0 and size-1, inclusive.
-
No integer will be appear more than once in disk.
Examples
0)    
{"3 4 5 6 8 9 10","17 16 15"}
20
Returns: 5
We can defrag the first file by moving the contents of sector 8 to sector 7, then 9 to 8, and finally 10 to 9. The second file can be defragged in a number of ways by moving the contents of two sectors, for a total of 5.
1)    
{"1 2 3 5 4 6 7 8"}
10
Returns: 2
Here we can take advantage of the fact that we have enough memory to hold two sectors worth of data. First, load the contents of sectors 4 and 5 into memory. Now, simply write the data back in the reverse order.
2)    
{"1 3 5 7","0 2 4 8","6 9"}
100
Returns: 7This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

解决方案 »

  1.   

    就是说要写个方法模拟磁盘整理int minMoves(String[] disk, int size)
    size是硬盘容量,disk array里有一些文件,每个文件占用一些sectors,如3 4 5 6 8 9 10
    内存只能放两个sectors,返回移动的次数,一定要移动的最少,这个怎么做啊?
      

  2.   

    看了一下原文和例子
    首先可以确定的是有三种动作是要计算的
    1.硬盘->硬盘
    2.硬盘->内存
    3.内存->硬盘个人感觉这个问题很难求出最优解,但是能求出较优解,应该用贪心方法解决
    首先根据每个文件的第一块sectors和其他sectors的顺序求出一个凌乱程度,
    根据凌乱程度由轻到重排出文件处理顺序,
    对于多个文件中sectors交叉的就
    1.硬盘->内存和内存->硬盘处理,将一个文件所需的sectors空出来,
    2.然后再硬盘->硬盘的排序;
    交叉执行1.2.直到该文件处理好为止,再处理下个文件...
      

  3.   

    非常感谢mooninsun() 的建议
    如果能给一段代码那就更好了
      

  4.   

    sqlink():
    我试一下吧,
    不过你要用的话,可能需要做进一步的改进