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 ...
随机推荐
- 【3】docker命令集
root@xcc-VirtualBox:/home/xcc# docker --helpUsage: docker [OPTIONS] COMMAND [arg...] docker [ ...
- js算数方法
原文:http://www.w3school.com.cn/js/js_obj_math.asp 除了可被 Math 对象访问的算数值以外,还有几个函数(方法)可以使用. 函数(方法)实例: 下面的例 ...
- 一个Cmake的例子
命令查询列表:http://www.cmake.org/cmake/help/v3.2/manual/cmake-commands.7.html # # Official dependency num ...
- Entity Framework技巧系列之六 - Tip 20 – 25
提示20. 怎样处理固定长度的主键 这是正在进行中的Entity Framework提示系列的第20篇. 固定长度字段填充: 如果你的数据库中有一个固定长度的列,例如像NCHAR(10)类型的列,当你 ...
- tsung: an open-source multi-protocol distributed load testing tool
ROPERTIES::type: KnowledgeBase_Cloud:END: 开源.多协议.分布式的压力测试工具 Item Summary tsung-recorder start 通过p ...
- 模拟摇奖:从1-36中随机抽出8个不重复的数字(math)
public class Yaojiang { public static void main(String[] args) { // TODO 自动生成的方法存根 int[] a=new int[8 ...
- Hadoop 中关于 map,reduce 数量设置
map和reduce是hadoop的核心功能,hadoop正是通过多个map和reduce的并行运行来实现任务的分布式并行计算,从这个观点来看,如果将map和reduce的数量设置为1,那么用户的任务 ...
- WEBROOT根目录 <%=request.getContextPath()%>
WEBROOT根目录 <%=request.getContextPath()%> == ${pageContext.request.contextPath}
- centos 6.2安装bind 9.8.2 master、slave与自动修改后更新
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dl528888.blog.51cto.com/2382721/1249311 随 ...
- 使WiFi具有保存历史连接的功能
在wpa_supplicant.conf里面添加这个功能 update_config=1 就能更新了,保存了历史的连接AP,不用再输入密码