博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1033
阅读量:5804 次
发布时间:2019-06-18

本文共 6097 字,大约阅读时间需要 20 分钟。

Defragment
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 2938   Accepted: 993
Case Time Limit: 1000MS   Special Judge

Description

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. 

Input

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. 

Output

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". 

Sample Input

20 34 2 3 11 121 73 18 5 10

Sample Output

2 13 211 312 418 610 85 207 520 7

Source

 
1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 int N,K; 9 int cluster[10010]; 10 int clusterNum; 11 stack
movStack; 12 int movNum; 13 14 void defrag() 15 { 16 bool isCycle; 17 int next; 18 19 for(int i=1; i<=N; i++) 20 { 21 if(cluster[i]==i) 22 { 23 continue; 24 } 25 else if(cluster[i]!=0) 26 { 27 movStack.push(i); 28 next=cluster[i]; 29 isCycle=false; 30 while(1) 31 { 32 if(cluster[next]==0) 33 { 34 isCycle=false; 35 break; 36 } 37 else if(cluster[next]==i) 38 { 39 isCycle=true; 40 break; 41 } 42 movStack.push(next); 43 next=cluster[next]; 44 } 45 46 int j, t; 47 48 if(isCycle) 49 { 50 for(j=N; j>=1; j--) 51 { 52 if(cluster[j]==0) 53 { 54 break; 55 } 56 } 57 printf("%d %d\n",next, j); 58 cluster[j]=cluster[next]; 59 60 while(!movStack.empty()) 61 { 62 t=movStack.top(); 63 printf("%d %d\n",t, next); 64 cluster[next]=cluster[t]; 65 next=t; 66 movNum++; 67 movStack.pop(); 68 } 69 cluster[next]=cluster[j]; 70 cluster[j]=0; 71 printf("%d %d\n",j, next); 72 } 73 else 74 { 75 while(!movStack.empty()) 76 { 77 t=movStack.top(); 78 printf("%d %d\n",t, next); 79 cluster[next]=cluster[t]; 80 next=t; 81 movNum++; 82 movStack.pop(); 83 } 84 cluster[next]=0; 85 } 86 87 } 88 } 89 90 if(movNum==0) 91 { 92 printf("No optimization needed"); 93 } 94 } 95 96 int main() 97 { 98 int t,initNum; 99 100 while(scanf("%d %d",&N,&K)!=EOF)101 {102 memset(cluster,0,(N+1)*sizeof(int));103 while(!movStack.empty())104 {105 movStack.pop();106 }107 movNum=0;108 clusterNum=1;109 for(int i=1; i<=K; i++)110 {111 scanf("%d",&t);112 while(t--)113 {114 scanf("%d",&initNum);115 cluster[initNum]=clusterNum;116 clusterNum++;117 }118 }119 120 defrag();121 }122 123 return 0;124 }

 

转载于:https://www.cnblogs.com/eric-blog/archive/2012/08/19/2646861.html

你可能感兴趣的文章
阿里云安全肖力:安全基础建设是企业数字化转型的基石 ...
查看>>
使用《Deep Image Prior》来做图像复原
查看>>
如何用纯 CSS 为母亲节创作一颗像素画风格的爱心
查看>>
Linux基础命令---rmdir
查看>>
优秀程序员共有的7种优秀编程习惯
查看>>
iOS sqlite3(数据库)
查看>>
粤出"飞龙",打造新制造广东样本
查看>>
编玩边学获数千万元A轮融资,投资方为君联资本
查看>>
开发者论坛一周精粹(第五十五期) 全站HTTPS之OSS教程 一次可以备案几个网站?...
查看>>
(干货)Linux学习资源推荐
查看>>
蓝图(Blueprint)详解
查看>>
Spark之SQL解析(源码阅读十)
查看>>
Android图片添加水印图片并把图片保存到文件存储
查看>>
C#字符串的不变性
查看>>
前端路由简介以及vue-router实现原理
查看>>
比特币系统采用的公钥密码学方案和ECDSA签名算法介绍——第二部分:代码实现(C语言)...
查看>>
分享15款很实用的 Sass 和 Compass 工具
查看>>
AMD优势: 与众不同 选择丰富
查看>>
玩转高性能超猛防火墙nf-HiPAC
查看>>
简单按日期查询mysql某张表中的记录数
查看>>