ural1037 Memory Management
Memory Management
Memory limit: 64 MB
Background
Problem
Input
<Time> +
<Time> . <BlockNo>
Output
- '+' 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).
Sample
input | output |
---|---|
1 + |
1 |
分析:树状数组来找当前第一个空闲内存;
优先队列来判断内存是否过期,注意优先队列里的时间不一定是实际到期时间,要注意判断;
代码:
#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的更多相关文章
- Memory Management in Open Cascade
Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- Objective-C Memory Management
Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...
- Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm
目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...
- 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- ...
- 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 ...
- Understanding Memory Management(2)
Understanding Memory Management Memory management is the process of allocating new objects and remov ...
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- 再谈.net的堆和栈---.NET Memory Management Basics
.NET Memory Management Basics .NET memory management is designed so that the programmer is freed fro ...
随机推荐
- webstoem自动编译less文件
去node的主页下载对应版本的nodejs然后安装下载地址:http://nodejs.org/ 根据自己的系统选择合适的版本下载. 安装完成之后打开命令提示符(win+r 输入cmd 回车),分 ...
- 自定义VBS脚本(统计在指定文件中搜索字符串出现的次数)
'=========================================================================='' VBScript Source File - ...
- hdu_5705_Clock("巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5705 题意:给你一个时间和一个角度,问你下一个时针和分针形成给出的角度是什么时候 题解:我们可以将这个 ...
- excel中自动变为插入语句的写法
=concatenate("insert into t(b,c) values('",b1,"','",c1,"');")
- Qt学习之系列[9] – QCoreApplication:processEvents()可能会引起递归,导致栈溢出崩溃
api含义:QCoreApplication::processEvents() 将处理所有事件队列中的事件并返回给调用者. 问题描述: 当主线程在某个槽函数里正在执行processEvents时, 刚 ...
- sql中 replace函数
例用 xxx 替换 abcdefghi 中的字符串 cde. SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')
- 不注册COM组件直接调用接口
本文以COM组件AppTest.dll为例,AppTest.dll中提供了ITest接口,在不使用regsvr32命令向系统注册的情况下创建ITest接口并调用. 一.导入组件或类型库: 在C++中使 ...
- 事件委托小demo(原生版)
<style type="text/css"> body, div, span { margin:; padding:; font-family: "\5FA ...
- Android OpenGL ES(十)绘制三角形Triangle .
三角形为OpenGL ES支持的面,同样创建一个DrawTriangle Activity,定义6个顶点使用三种不同模式来绘制三角形: float vertexArray[] = { -0.8f, - ...
- 火狐解决OCSP回应包含过期信息的问题
连接 addons.mozilla.org 时发生错误. OCSP 回应包含过期信息. (错误码: sec_error_ocsp_old_response)hosts文件添加 vi /etc/host ...