求磁盘碎片整理算法

treeroot 2005-08-14 12:01:02
这个是我昨天在topcoder(googlejam) 练习室看道的一道算法题,我昨天想了一天
最后得出一个结论:我想出了一个笨方法,但是不可以用(复杂度为m的n次方),简直
是天文数字,所以我做不出来,原题如下,希望大家能提供一些资料,给点提示.
执行不能超过2秒!

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: 7

This 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.
...全文
288 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jstxqgb 2005-08-23
  • 打赏
  • 举报
回复
好像比较麻烦哦,要解决最短时间问题还有死锁问题一大堆乱七八糟的算法。
看看windows为什么硬盘空间低于15%空间就不允许碎片整理了,算法太麻烦了干脆就不算了。
lz慢慢研究,感觉不是一两天能研究出来的东西。。。
llly1314 2005-08-22
  • 打赏
  • 举报
回复
与其说他是想做一个磁盘整理的程序
到不如说他只是想研究算法而已!
Preamble 2005-08-22
  • 打赏
  • 举报
回复
算法是灵魂!
佩服楼主的English。
也佩服楼主的水平
yezi09 2005-08-22
  • 打赏
  • 举报
回复
佩服佩服,看题目看了我半天!
donnydream 2005-08-21
  • 打赏
  • 举报
回复
用JAVA写?Java的优势不在这里,到C或C++板块看看
TalentXu 2005-08-18
  • 打赏
  • 举报
回复
楼上几位可能没看题吧,这道题根硬件一点关系都没有,纯粹在一个基于磁盘整理的简化模型下讨论算法而已
homesos 2005-08-18
  • 打赏
  • 举报
回复
不懂,这个问题太深奥了

帮顶
tomuno 2005-08-17
  • 打赏
  • 举报
回复
要做一个磁盘整理
windows自带磁盘整理工具,而且还为其他整理工具提供接口
感觉磁盘整理要和操作系统紧密联系
因为不同操作系统的文件管理和目录管理有所不同,你要做的磁盘整理不可能直接和硬件打交道吧?
qxbnit 2005-08-16
  • 打赏
  • 举报
回复
这个。。。。
跟硬件贴边太近了,可怕
======================
老大,你的E文佩服
zzzle 2005-08-16
  • 打赏
  • 举报
回复
两个星的就是牛
hui_feng 2005-08-15
  • 打赏
  • 举报
回复
强,帮你顶一下。
flyxxxxx 2005-08-15
  • 打赏
  • 举报
回复
java区应该很少有搞这个的,你应该去C/C++区
chenweionline 2005-08-14
  • 打赏
  • 举报
回复
顶个

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧