码迷,mamicode.com
首页 > 其他好文 > 详细

poj1033:Defragment

时间:2014-07-02 14:39:59      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

总时间限制: 2000ms 内存限制: 65536kB
描述
You are taking part in the development of a "New Generation" operating system and the NG file system. In this file system all disk space is divided into N clusters of the equal sizes, numbered by integers from 1 to N. Each file occupies one or more clusters in arbitrary areas of the disk. All clusters that are not occupied by files are considered to be free. A file can be read from the disk in the fastest way, if all its clusters are situated in the successive disk clusters in the natural order.
Rotation of the disk with constant speed implies that various amounts of time are needed for accessing its clusters. Therefore, reading of clusters located near the beginning of the disk performs faster than reading of the ones located near its ending. Thus, all files are numbered beforehand by integers from 1 to K in the order of descending frequency of access. Under the optimal placing of the files on the disk the file number 1 will occupy clusters 1, 2, ..., S1, the file number 2 will occupy clusters S1+1, S1+2, ..., S1+S2 and so on (here Si is the number of clusters which the i-th file occupies).
In order to place the files on the disk in the optimal way cluster-moving operations are executed. One cluster-moving operation includes reading of one occupied cluster from the disk to the memory and writing its contents to some free cluster. After that the first of them is declared free, and the second one is declared occupied.
Your goal is to place the files on the disk in the optimal way by executing the minimal possible number of cluster-moving operations.
输入
The first line of the input file contains two integers N and K separated by a space(1 <= K < N <= 10000).Then K lines follow, each of them describes one file. The description of the i-th file starts with the integer Si that represents the number of clusters in the i-th file (1 <= Si < N). Then Si integers follow separated by spaces, which indicate the cluster numbers of this file on the disk in the natural order.
All cluster numbers in the input file are different and there is always at least one free cluster on the disk.
输出
Your program should write to the output file any sequence of cluster-moving operations that are needed in order to place the files on the disk in the optimal way. Two integers Pj and Qj separated by a single space should represent each cluster-moving operation. Pj gives the cluster number that the data should be moved FROM and Qj gives the cluster number that this data should be moved TO. 
The number of cluster-moving operations executed should be as small as possible. If the files on the disk are already placed in the optimal way the output should contain only the string "No optimization needed".
样例输入
20 3
4 2 3 11 12
1 7
3 18 5 10
样例输出
2 1
3 2
11 3
12 4
18 6
10 8
5 20
7 5
20 7

先写了一个弱B版本的,还用了stl的队列来实现在一部分测试集上过了,在百练上就挂了。。。

速度慢就不说了,还WA了。。。

这道题我觉得题目没说明白,导致了我对这道题目理解出现了很大误区,其实这道题只要输出的结果合理就能AC了。

大致思路就是:用深搜,如果这个块儿应该放入的块儿还没有被用过,那么直接将该块儿放入应该放入的块儿,否则就去判断该块儿应该放入的那个块儿应该放入的块儿有没有被用过。

说起来稍微有点绕。

见代码

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 int n,k,i;
 6 int a[10001]={0};
 7 
 8 int f=0;
 9 void work(int x)
10 {
11     if(a[a[x]] == 0)
12     {
13         printf("%d %d\n",x,a[x] );
14         a[a[x]] = -1;
15         a[x] = 0;
16         return ;
17     }
18     if(a[x] == f)
19     {
20         int j;
21         for(j = 1; j <= n; ++j)
22         {
23             if(a[j] == 0)
24                 break;
25         }
26         a[j] = a[f];
27         a[f] = 0;
28         printf("%d %d\n",f,j );
29         a[f] = -1;
30         a[x] = 0;
31         printf("%d %d\n",x,f );
32         if(j < i)
33             i = j-1;
34     }
35     else
36     {
37         work(a[x]);
38         if(a[x] == -1)
39             return ;
40         else
41         {
42             printf("%d %d\n",x,a[x] );
43             a[a[x]] =-1;
44             a[x] = 0;
45         }
46     }
47 }
48 
49 int main()
50 {
51     scanf("%d %d",&n,&k);
52     int coun = 0;
53     for( i = 0; i < k; ++i)
54     {
55         int m;
56         scanf("%d",&m);
57         for(int j = 0; j < m; ++j)
58         {
59             coun++;
60             int x;
61             scanf("%d",&x);
62             a[x] = coun;
63             if(x == coun)
64                 a[x] = -1;
65         }
66     }
67     
68     for( i = 1; i <= n; ++i)
69     {
70         if(a[i] != -1 && a[i] != 0)
71         {
72             f = i;
73             work(i);
74         }
75     }
76     if(f == 0)
77     {
78         printf("No optimization needed\n");
79     }
80     return 0;
81 }
View Code

 

poj1033:Defragment,布布扣,bubuko.com

poj1033:Defragment

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/xiaoshen555/p/3819373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!