Memory Management

Time limit: 2.0 second
Memory limit: 64 MB

Background

Don't you know that at school pupils’ programming contest a new computer language has been developed. We call it D++. Generally speaking it doesn't matter if you know about it or not. But to run programs written in D++ we need a new operating system. It should be rather powerful and complex. It should work fast and have a lot of possibilities. But all this should be done in a future.
And now you are to… No. You should not devise the name for the operating system. You are to write the first module for this new OS. And of course it's the memory management module. Let's discuss how it is expected to work.

Problem

Our operating system is to allocate memory in pieces that we’ll call “blocks”. The blocks are to be numbered by integers from 1 up to N. When operating system needs more memory it makes a request to the memory management module. To process this request the memory management module should find free memory block with the least number. You may assume that there are enough blocks to process all requests.
Now we should define the meaning of words “free block”. At the moment of first request to the memory management module all blocks are considered to be free. Also a block becomes free when there were no requests to it during T minutes.
You may wonder about a notion “request to allocated blocks”. What does it mean, “request to allocated block”? The answer is simple: at any time the memory management module may be requested to access a given block. To process this request the memory management module should check if the requested block is really allocated. If it is, the request is considered to be successful and the block remains allocated for T minutes more. Otherwise the request is failed.
That's all about the algorithms of the memory management block. You are to implement them for N = 30 000 and T = 10 minutes.

Input

Each line of input contains a request for memory block allocation or memory block access. Memory allocation request has a form:
<Time> +
where <Time> is a nonnegative integer number not greater than 65 000. Time is given in seconds. Memory block access request has a form:
<Time> . <BlockNo>
where <Time> meets conditions mentioned above for the memory allocation request and <BlockNo> is an integer value in range from 1 to N. There will be no more than 80000 requests.

Output

For each line of input you should print exactly one line with a result of request processing. For memory allocation request you are to write an only integer — a number of allocated block. As it was mentioned above you may assume that every request can be satisfied, there will be no more than Nsimultaneously allocated blocks. For memory block access request you should print the only character:
  • '+' if request is successful (i.e. block is really allocated);
  • '-' if request fails (i.e. block with number given is free, so it can't be accessed).
Requests are arranged by their times in an increasing order. Requests with equal times should be processed as they appear in input.

Sample

input output
1 +
1 +
1 +
2 . 2
2 . 3
3 . 30000
601 . 1
601 . 2
602 . 3
602 +
602 +
1202 . 2
1
2
3
+
+
-
-
+
-
1
3
-

分析:树状数组来找当前第一个空闲内存;

   优先队列来判断内存是否过期,注意优先队列里的时间不一定是实际到期时间,要注意判断;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=3e4+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,now[maxn],a[maxn];
void add(int x,int y)
{
for(int i=x;i<=maxn-;i+=(i&(-i)))
a[i]+=y;
}
int get(int x)
{
int res=;
for(int i=x;i;i-=(i&(-i)))
res+=a[i];
return res;
}
struct node
{
int t,id;
node(int _t,int _id)
{
t=_t,id=_id;
}
bool operator<(const node&p)const
{
return t>p.t;
}
};
char op[];
priority_queue<node>q;
int main()
{
int i,j;
memset(now,-,sizeof now);
while(~scanf("%d",&n))
{
scanf("%s",op);
if(op[]=='.'){
scanf("%d",&m);
if(now[m]>=n)
{
now[m]=n+;
puts("+");
}
else puts("-");
}
else
{
while(!q.empty()&&q.top().t<n){
if(now[q.top().id]<n)add(q.top().id,-);
else q.push(node(now[q.top().id],q.top().id));
q.pop();
}
int l=,r=,ret;
while(l<=r)
{
int mid=l+r>>;
if(get(mid)<mid)ret=mid,r=mid-;
else l=mid+;
}
add(ret,);
now[ret]=n+;
q.push(node(now[ret],ret));
printf("%d\n",ret);
}
}
//system("pause");
return ;
}

ural1037 Memory Management的更多相关文章

  1. Memory Management in Open Cascade

    Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...

  2. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

  3. Objective-C Memory Management

    Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...

  4. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  5. Android内存管理(2)HUNTING YOUR LEAKS: MEMORY MANAGEMENT IN ANDROID PART 2

    from: http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of- ...

  6. Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1

    from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...

  7. Understanding Memory Management(2)

    Understanding Memory Management Memory management is the process of allocating new objects and remov ...

  8. Java Memory Management(1)

    Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...

  9. 再谈.net的堆和栈---.NET Memory Management Basics

    .NET Memory Management Basics .NET memory management is designed so that the programmer is freed fro ...

随机推荐

  1. 转:Jmeter之使用CSV Data Set Config实现参数化登录

    在使用Jemeter做压力测试的时候,往往需要参数化用户名,密码以到达到多用户使用不同的用户名密码登录的目的.这个时候我们就可以使用CSV Data Set Config实现参数化登录: 首先通过Te ...

  2. WEB 端批量移动设备管理控制工具 STF 的环境搭建和运行

    最近项目涉及到较多设备批量管理的需求,发现一工具,可以批量对大量设备进行WEB端管理,工具主页:https://openstf.github.io/工具名STF(Smartphone Test Far ...

  3. 使用CodeFirst实现动态建库

    一.业务分析 以我们平时注册今目标为例,我们在注册今目标的过程中,具体步骤是这样的: 图1 今目标登陆流程 详细解释一下: 第一步:注册界面.输入手机号或者邮箱,点击确定进入基本信息界面. 第二步:基 ...

  4. tar解压

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:-x : 解压缩压缩档案的参数:-z : 是否需 ...

  5. javascript模糊查询一个数组

    /* * 模糊查询一个数组 */ com.ty.repairtech.JsonOperation.searchList = function(str, container) { var newList ...

  6. php过滤提交数据 防止sql注入攻击

    规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...

  7. 关于安卓HTTP请求用HttpUrlConnection还是HttpClient好

    安卓和JAVA应用开发少不了要提交HTTP请求,而基本上目前有两个实现方式:HttpUrlConnection(即URL.openConnection)和HttpClient. 网上不少人都认为Htt ...

  8. inno setup 1

      1.简单脚本 [setup] AppName=Test AppVerName=Test DefaultDirName="d:\setup\app" AppVersion=1.0 ...

  9. gnome3

    http://askubuntu.com/questions/67753/how-do-i-add-an-application-to-the-dash https://wiki.gnome.org/ ...

  10. JavaBean技术的一些讲解

    JavaBean: 由于原先的jsp的html代码和css代码以及java代码都是写在一起的,所以看起来就比较冗余,同时 也体现不了javaBean中面向对象的思想的{当然可以同过jstl标签库以及m ...