POJ 1033 / Northeastern Europe 1998 Defragment (模拟&队列)

Northeastern Europe (NEERC)1997 测试数据 立即下载
Defragment

Time Limit:  2000MS
Memory Limit: 10000K
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 3
4 2 3 11 12
1 7
3 18 5 10

Sample Output

2 1
3 2
11 3
12 4
18 6
10 8
5 20
7 5
20 7


大模拟。详见代码。
为何这样做是最优的?因为如此移动到最后,最开始移出到外面的那个碎片会回到它想去的地方。

完整代码:
/*172ms,256KB*/

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int source[10001];///该簇所放碎片之前的簇位置
int target[10001];///该簇碎片的目标簇位置
queue<int> q;

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		memset(source, 0, sizeof(source));
		memset(target, 0, sizeof(target));
		int s, count = 0, i = 1, j, t = 1; ///count为总碎片数
		bool tidy = false;
		while (k--)
		{
			scanf("%d", &s);
			count += s;
			while (s--)
			{
				scanf("%d", &j);
				source[i] = j;
				target[j] = i;
				++i;
			}
		}
		for (i = 1; i <= count; ++i)
			if (target[i] == 0)///空位置
				q.push(i);
		if (!q.empty())
			tidy = true;
		while (!q.empty())
		{
			i = q.front();
			q.pop();
			j = source[i];
			printf("%d %d\n", j, i);
			source[i] = i;
			target[i] = i;///更新
			target[j] = 0;
			if (j <= count)
				///多出的空位置放后面,所以我们先把能直接移动的都移动了,再处理后续要移动的
				q.push(j);
		}
		for (i = 1; i <= count; ++i)
			if (target[i] != i)
			{
				if (!tidy)
					tidy = true;
				printf("%d %d\n", i, count + 1);///先移到末尾
				target[count + 1] = target[i];
				source[target[i]] = count + 1;
				target[i] = 0;
				t = i;
				///连续整理
				while (t <= count)
				{
					j = source[t];
					printf("%d %d\n", j, t);
					target[t] = t;
					source[t] = t;
					target[j] = 0;
					t = j;
				}
			}
		if (!tidy)
			puts("No optimization needed");
	}
}


python爬虫入门教程(非常详细) 爬虫指的是一种自动化程序,能够模拟人类在互联网上的浏览行为,自动从互联网上抓取、预处理并保存所需要的信息。爬虫运行的过程一般是先制定规则(如指定要抓取的网址、要抓取的信息的类型等),紧接着获取该网址的HTML源代码,根据规则对源代码进行解析和抽取,最后进行处理和保存。爬虫在实际应用中广泛使用,如搜索引擎、大数据分析、交易数据采集等领域,都需要用到爬虫技术来实现信息的定向采集和处理。 阅读详情

相关推荐

Linux USB设备命名规则

有一个USB设备,接到了Linux系统的终端的某个插口上,那么怎么找到这个设备对应设备路径呢?这个USB设备虚拟了多个功能(三个串口和一个网卡), 如何找到对应操作路径呢?

weixin_43132521的博客 1698

UVA 669 - Defragment(dfs)

Defragment  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 eq

Remilia's 1631

别再死记硬背了!用51单片机的AD/DA和PWM,亲手做个简易示波器信号发生器

本文详细介绍了如何利用51单片机的PWM和AD/DA功能制作简易示波器信号发生器。通过PWM模拟DAC输出波形,结合XPT2046 ADC进行信号测量,实现方波、三角波和正弦波的生成与显示。项目不仅成本低廉,还能帮助电子爱好者深入理解51单片机的核心功能,是理论与实践结合的绝佳案例。

weixin_28675839的博客 265

【POJ】【1033】【Defragment】【模拟】解题报告

【POJ】【1033】【Defragment】【模拟】解题报告   地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1033   点评:此题的通过率看起来挺吓人的,其实也不难。   题目大意:你要写一个OS,要实现磁盘碎片整理的功能。磁盘分为N个簇,一个文件可以占用K个簇,(1 &lt;= K...

weixin_33691700的博客 394

CANoe数据回放实战:如何用Replay Block精准复现实车故障(附ECU数据处理技巧)

本文深入探讨了利用CANoe的Replay Block功能进行数据回放,以精准复现实车偶发性故障的实战方法。文章重点解析了从原始OBD日志中移除目标ECU数据、转换报文方向等核心数据处理技巧,旨在帮助工程师重构真实的网络测试环境,从而高效定位并解决复杂的ECU问题。

nnn11的博客 496

manimgl安装

安装manimgl

happytree001的博客 707

poj 1033

题目 Defragment Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 4450   Accepted: 1557 Case Time Limit: 1000MS   Special Judge Description You are

PaladinDu的专栏 478

FPGA实现PI控制

FPGA实现PI控制

m0_57319166的博客 3895

POJ 1032 / Northeastern Europe 1998 Parliament (贪心)

http://poj.org/problem?id=1032 题意:把N个人分成若干组且每组人数不同,每天每组派一个人出来开会,并且每天开会的这群人不与前面的日子的开会人群不完全相同。 也就是说,求N1+N2+...+Nn=N,使N1,N2,...,Nn都不相等且他们的乘积最大。 思路:分的组越多乘积越大(在题意下ab>a+b) 则有分法:设有一连续递增序列为2,3

AC,∑ndless 1518

基于STM32的Modbus TCP多主站数据监测

Modbus TCP通信协议在STM32上实现,STM32作为服务器采集内部传感器数据,并通过ADC进行转换,存入寄存器。两个PC机作为客户端访问服务器,服务器通过Socket通信实现监听连接,将数据发送给客户端。

hyp12347的博客 4294

PSM倾向性评分分析介绍、spss进行倾向性评分分析示例

PSM倾向性评分分析介绍、spss进行倾向性评分分析示例

statistics+insight+vista+power 1481

ACM题目中输入数据的处理(C语言版)

ACM题目中输入数据的处理(C++版),见:http://blog.youkuaiyun.com/sxhelijian/article/details/8978850  ACM竞赛题目的输入数据常要求有多组,并且格式多种多样,这是初次登OJ平台的同学的一个障碍。实际上,这些格式可以归为固定的几种类型,本文介绍各种类型的处理方法,以帮助同学们克服这些障碍。  实际上,这些模式不仅是OJ平台上做题的需要。在平时的

迂者-贺利坚的专栏 3万+

11、机器学习中的聚类与隐马尔可夫模型

本文介绍了机器学习中的两种重要方法:聚类算法和隐马尔可夫模型(HMM)。聚类作为无监督学习的代表,适用于发现无标签数据中的结构,常见算法包括K-均值和自组织映射(SOM),并在传感器数据分析等场景中展现应用价值。隐马尔可夫模型则是一种可解释的序列模型,用于从观测数据中推断隐藏状态,广泛应用于语音识别、自然语言处理和生物信息学等领域。文章详细讲解了HMM的核心概念如马尔可夫性质、转移与发射概率,并通过TensorFlow实现了前向算法,展示了其计算观测序列概率的动态规划思想。最后对比了两类方法的应用场景,展望

garlic的博客 179

poj 1032 Parliament

Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16784   Accepted: 7082 Description New convocation of The Fool Land's Parliament consists of N del

尘飞——Data Scientist 1083

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

UVa 669 - Defragment(DFS)

题意 这题看了好久才懂什么意思。 简单地说就是给出一个数字原来的位置,和最后的位置,求移动顺序。 输入簇(cluster)的总数和文件个数,每个文件个数后面跟着的是各个簇的原始位置。最后要按顺序排下来。 思路 分两种情况。 如果没有环,直接DFS然后换掉。 如果有环,从后面开始找到第一个空的位置,然后把数移过去,然后就递归了。

一只冰熊 971

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

mysql出现1033错误的处理办法

mysql 出现: SQL 查询: SHOW INDEX FROM `kb_ad` ; MySQL 返回: #1033 - Incorrect information in file: “./darkwoodata/kb_ad.frm” 原因:数据库中大部分表都是 MyISAM引擎的,而新建的表默认是用的InnoDB引擎,本来是没有问题的,用InnoDB也可以正常运行

gzidcorg的专栏 5471

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Northeastern Europe 2007, Northern Subregion History Of Football

【题目描述】n只球队打锦标赛,两两之间进行一场比赛,胜积3分,平积1分,负积0分,现在给出n只球队最后的得分,问可能的比赛结果有多少种? 【输入输出】n (2 n行,每行一个整数,表示某队的得分输出可能的结果数 下面是样例3333 2-------------------------------Possible table numbe

guowenzhang的专栏 856

Drying (Poj3104,Northeastern Europe 2005, Northern Subregion)

Drying Poj3104,二分

Leo的博客 716

Ants(Northeastern Europe 2007)

Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3539 Accepted: 1064 Special Judge Description Young naturalist Bill studies ants in s...

xidajiancun 178

[伯努利数] poj 1707 Sum of powers

题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 735   Accepted: 354 D

cc_again的专栏 1713

UVa 414 Machined Surfaces (water ver.)

414 - Machined Surfaces Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=355 An imaging device furnishes di

AC,∑ndless 6714

POJ 1835 宇航员 (模拟&三维向量旋转)

宇航员 http://poj.org/problem?id=1835 Time Limit: 2000MS Memory Limit: 30000K Description 问题描述:    宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下

AC,∑ndless 3250

UVa 118 Mutant Flatworld Explorers (模拟机器人移动)

118 - Mutant Flatworld Explorers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=54 旋转时+1或+3再取模即可。 完整

AC,∑ndless 2570

Codeforces Beta Round #74 (Div. 2) / 90B African Crossword (模拟)

B. African Crossword http://codeforces.com/problemset/problem/90%2FB time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard o

AC,∑ndless 2416
上一篇: POJ 1365 / Central Europe 1997 Prime Land (数论)
下一篇: POJ 3126 / Northwestern Europe 2006 Prime Path (数论&BFS)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值