poj1033:Defragment

总时间限制: 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了。

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

说起来稍微有点绕。

见代码

 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 }

poj1033:Defragment

时间: 2024-10-28 10:08:07

poj1033:Defragment的相关文章

UVA - 669 Defragment

题意:简单说就是将第i个簇号放回i,求最少的步数 思路:只处理链形,和环形的情况,其他的可以不管,对于链形来说,只要倒置就行了,环形的找一个空闲的放一个,然后就是链形的情况了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> using namespace std; const int MAXN = 1000

poj 1033 Defragment

题解转自:http://www.cnblogs.com/damacheng/archive/2010/09/24/1833983.html 题目大意:你要写一个OS,要实现磁盘碎片整理的功能.磁盘分为N个簇,一个文件可以占用K个簇,(1 <= K < N <= 10000),给出各个文件的占用磁盘的情况,也就是一个文件占用了哪些簇,想要进行碎片整理,就是把这些簇按顺序整理到磁盘的最顶部,例如给出示例: 文件1:2 3 11 12,占用了4个簇,编号为1-4. 文件2:7,占用了1个簇,编

1033:Defragment 模拟

描述您正在参与“新一代”操作系统和NG文件系统的开发.在该文件系统中,所有磁盘空间被分成相等大小的N个簇,由1到N的整数编号.每个文件占据盘的任意区域中的一个或多个簇.未被文件占用的所有群集都被视为免费.如果文件的所有集群都以自然顺序位于连续的磁盘集群中,则可以以最快的方式从磁盘读取文件.以恒定速度旋转磁盘意味着访问其集群需要不同的时间量.因此,读取位于磁盘开头附近的簇比读取位于其末端附近的簇执行得更快.因此,所有文件都按照从1到K的整数按照访问频率下降的顺序编号.在文件在磁盘上的最佳放置下,文

oracle数据库ORA-01654 错误的解决方法

引言: 数据库突然报: ORA-01654: unable to extend index BO.INDEX_indexname by 311072 in tablespace 错误,上网查原因,发现解决方法只有一个,就 是增加tablespace的大小.现归纳解决此问题的方法如下. 方法1: 当出现类似错误时,首先检查tablespace的空间是否足够大,如果不够大,说明tablespace的空间不够扩展了,这时候需要将tablespace的datafile的 size变大,方法很简单我就不讲

hog源码分析

http://www.cnblogs.com/tornadomeet/archive/2012/08/15/2640754.html 在博客目标检测学习_1(用opencv自带hog实现行人检测) 中已经使用了opencv自带的函数detectMultiScale()实现了对行人的检测,当然了,该算法采用的是hog算法,那么hog算法是怎样实现的呢?这一节就来简单分析一下opencv中自带 hog源码. 网上也有不少网友对opencv中的hog源码进行了分析,很不错,看了很有收获.比如: htt

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

批处理基本知识以及进阶 V2.0

批处理基本知识以及进阶 将以要执行的程序指令 , 像在 dos 模式下一下写入记事本 , 保存成 bat 文件 , 就可以执行了 一 . 简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数, echo 命令将显示当前回显设置. 语法 : echo [{on │ off}] [message] Sample : @echo off / echo hello world 在实际应用中我们会把这条命令和重定向符号(也称为管道符号,一般用 > >>

Heap Only Tuples (HOT)

Introduction ------------ The Heap Only Tuple (HOT) feature eliminates redundant index entries and allows the re-use of space taken by DELETEd or obsoleted UPDATEd tuples without performing a table-wide vacuum. It does this by allowing single-page va

HANA SQL

约束 注释 你可以给你的 SQL 语句添加注释来增加可读性和可维护性. SQL 语句中注释的分隔如下: l  双连字符“--”.所有在双连字符之后直到行尾的内容都被 SQL 解析器认为是注释. l  “/*”和“*/”.这种类型的注释用来注释多行内容.所有在引号符“/*”和关闭符“*/”之间 的文字都会被 SQL 解析器忽略. 标识符 标识符用来表示 SQL 语句中的名字,包括表名.视图名.同义字.列名.索引名.函数名.存储过程名.用户名.角色名等等.有两种类型的标识符:未分隔标识符和分隔标识符