Allocation of Memory 解题报告
来源:互联网 发布:java数据库连接池代码 编辑:程序博客网 时间:2024/06/04 19:03
第1484题,Allocation of Memory,题目链接http://202.197.224.59/OnlineJudge/index.php/problem/read/id/1484,直接暴力模拟。收到Quit信号,即清空内存为0。否则,察看如果有空间,若有,直接使用,赋值为pid,输出内存地址;否则,碎片整理,再看是否有空间,若有,直接使用,赋值为pid,输出内存地址;若无,输出-1。
C语言代码如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#define MAX_MEMORY 510typedef int COUNT;int memory[MAX_MEMORY];int maxMemory;bool canAlloc( int start, int apply ){COUNT i;for ( i = 0 ; i < apply ; i ++ ){if ( start + i > maxMemory )return false;if ( memory[start+i] != 0 )return false;}return true;}void defrag(void){COUNT i, j;for ( i = 1 ; i <= maxMemory ; i ++ ){if ( memory[i] == 0 ){for ( j = i + 1 ; j <= maxMemory ; j ++ ){if ( memory[j] != 0 ){memory[i] = memory[j];memory[j] = 0;break;}}}}}int alloc( int pid, int apply ){COUNT i, j;for ( i = 1 ; i <= maxMemory ; i ++ ){if ( memory[i] == 0 ){if ( canAlloc(i, apply) ){for ( j = 0 ; j < apply ; j ++ )memory[i+j] = pid;return i;}}}defrag();for ( i = 1 ; i <= maxMemory ; i ++ ){if ( memory[i] == 0 ){if ( canAlloc(i, apply) ){for ( j = 0 ; j < apply ; j ++ )memory[i+j] = pid;return i;}}}return -1;}void killprocess( int pid ){COUNT i;for ( i = 1; i <= maxMemory ; i ++ ){if ( memory[i] == pid )memory[i] = 0;}}int main (void){int testcases;COUNT i, j;char cmd[12];int pid, apply;int instructions;scanf( "%d", &testcases );for ( i = 1 ; i <= testcases ; i ++ ){scanf( "%d%d", &maxMemory, &instructions );memset( memory, 0, sizeof(memory) );printf( "Case %d:\n", i );for ( j = 0 ; j < instructions ; j ++ ){scanf( "%s", cmd );if ( !strcmp( cmd, "New" ) ){scanf( "%d%d", &pid, &apply );printf( "%d\n", alloc( pid, apply ) );}else{scanf( "%d", &pid );killprocess( pid );}}}return EXIT_SUCCESS;}
- Allocation of Memory 解题报告
- Memory Allocation of C++
- pku 1129 channel allocation 回溯解题报告
- POJ-1129 Channel Allocation 解题报告
- UVALive 5864 Register Allocation 解题报告
- POJ-1129 Channel Allocation 解题报告
- Android eglCreateWindowSurface Memory Allocation Kinds of Buffers Memory Share
- Memory Allocation
- XiangTan Univ. invitational Contest 2010Allocation of Memory
- Out of memory on a 23040016-byte allocation.的原因
- android Out of memory on a xxx-byte allocation问题
- Android Out of memory on a 5200288-byte allocation.解决方法
- Out of memory on a 11111-byte allocation
- New Memory allocation 1046759 bytes is smaller than the minimum allocation size of 1048576 bytes.
- Memory allocation with strings
- DomUs and memory allocation
- Windows Memory Allocation Limitations
- Advanced Memory Allocation
- STM32启动代码概述
- 无法打开文件“…….lib, 报错,vs2008
- ModelSim SE中wlf文件的一些问题(打不开wlf文件,文件根目录下有一大堆没有扩展名的文件)
- zoj 3469 dp 记忆化搜索
- 好书,准备要看
- Allocation of Memory 解题报告
- JAR包及jar命令用法
- ロシア靑年無メンテナンス写真(図)に刺激を求めて
- Web Application 和 Web Service 的区别与联系
- 父类子类的静态初始化块,初始化块,构造器执行顺序
- 委托加载DLL
- HashMap 和 Hashtable 的区别
- OpenSessionInViewFilter的一些原理
- ucosII 内存管理 解析