当前位置:网站首页>HDU-Memory Control
HDU-Memory Control
2022-04-23 05:51:00 【Round moon】
Memory Control
题目描述
Memory units are numbered from 1 up to N.
A sequence of memory units is called a memory block.
The memory control system we consider now has four kinds of operations:
- Reset Reset all memory units free.
- New x Allocate a memory block consisted of x continuous free memory units with the least start number
- Free x Release the memory block which includes unit x
- Get x Return the start number of the xth memory block(Note that we count the memory blocks allocated from left to right)
Where 1<=x<=N.You are request to find out the output for M operations.
输入描述
Input contains multiple cases.
Each test case starts with two integer N,M(1<=N,M<=50000) ,indicating that there are N units of memory and M operations.
Follow by M lines,each line contains one operation as describe above.
输出描述
For each “Reset” operation, output “Reset Now”.
For each “New” operation, if it’s possible to allocate a memory block,
output “New at A”,where Ais the least start number,otherwise output “Reject New”.
For each “Free” operation, if it’s possible to find a memory block occupy unit x,
output “Free from A to B”,where A and B refer to the start and end number of the memory block,otherwise output “Reject Free”.
For each “Get” operation, if it’s possible to find the xth memory blocks,
output “Get at A”,where A is its start number,otherwise output “Reject Get”.
Output one blank line after each test case.
Sample Input
6 10
New 2
New 5
New 2
New 2
Free 3
Get 1
Get 2
Get 3
Free 3
Reset
Sample Output
New at 1
Reject New
New at 3
New at 5
Free from 3 to 4
Get at 1
Get at 5
Reject Get
Reject Free
Reset Now
题目大意
多组输入
首先输入一个数n代表内存长度,接下来输入一个m,代表有m条操作。
有四个操作
第一个是Reset操作,会将内存全部清空。即释放全部内存
第二个操作New操作,获取长度为x的内存,并输出第一个内存的所在地址。如果不能则输出拒绝创建
第三个操作Free操作,释放x所在的内存段,注意x不一定是首地址。如果当前不需要释放则输出拒绝释放
第四个操作,Get操作,输出第x段内存段的首地址,如果没有第x段内存,则输出拒绝获取。
题目分析
这显然是一个区间修改的合并区间问题的线段树。这样Reset和New操作很好写,但是Free操作有些问题。我们可以创建一个vector,如果New一个新区间,那么我们插入到合适的位置,保证vector整体有序。好我们可以用二分去查询Free的首地址了。同时Get操作直接访问vector元素即可。
AC代码
//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
return a * b / gcd(a, b);
}
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a > 9)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define read read()
const int N = 50000;
struct node {
ll sum, rmaxn, lmaxn;
int lazy;
}tree[N * 4 + 7];
void creat(int l, int r, int rt)
{
int len = r - l + 1;
int mid = l + r >> 1;
tree[rt] = node{
len,len,len,0 };
if (l == r)return;
creat(l, mid, rt << 1);
creat(mid + 1, r, rt << 1 | 1);
}
void push_up(int l, int r, int rt)
{
int mid = l + r >> 1;
if (tree[rt << 1].sum == mid - l + 1)
tree[rt].lmaxn = tree[rt << 1].sum + tree[rt << 1 | 1].lmaxn;
else
tree[rt].lmaxn = tree[rt << 1].lmaxn;
if (tree[rt << 1 | 1].sum == r - mid)
tree[rt].rmaxn = tree[rt << 1 | 1].sum + tree[rt << 1].rmaxn;
else
tree[rt].rmaxn = tree[rt << 1 | 1].rmaxn;
tree[rt].sum = tree[rt << 1].rmaxn + tree[rt << 1 | 1].lmaxn;
tree[rt].sum = max(tree[rt << 1 | 1].sum, max(tree[rt << 1].sum, tree[rt].sum));
}
void push_down(int l, int r, int rt)
{
int mid = l + r >> 1;
if (tree[rt].lazy)
{
if (tree[rt].lazy == 1)
{
tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = 1;
tree[rt << 1].lmaxn = tree[rt << 1].rmaxn = tree[rt << 1].sum = 0;
tree[rt << 1 | 1].lmaxn = tree[rt << 1 | 1].rmaxn = tree[rt << 1 | 1].sum = 0;
}
else
{
tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = 2;
tree[rt << 1].lmaxn = tree[rt << 1].rmaxn = tree[rt << 1].sum = mid - l + 1;
tree[rt << 1 | 1].lmaxn = tree[rt << 1 | 1].rmaxn = tree[rt << 1 | 1].sum = r - mid;
}
tree[rt].lazy = 0;
}
}
void update(int L, int R, int mark, int l, int r, int rt)
{
if (L <= l && r <= R)
{
if (mark == 1)
tree[rt].sum = tree[rt].lmaxn = tree[rt].rmaxn = 0;
else
tree[rt].sum = tree[rt].lmaxn = tree[rt].rmaxn = r - l + 1;
tree[rt].lazy = mark;
return;
}
if (tree[rt].lazy)push_down(l, r, rt);
int mid = l + r >> 1;
if (L <= mid)update(L, R, mark, l, mid, rt << 1);
if (R > mid)update(L, R, mark, mid + 1, r, rt << 1 | 1);
push_up(l, r, rt);
}
ll query(int len, int l, int r, int rt)
{
if (l == r)return l;
if (tree[rt].lazy)push_down(l, r, rt);
int mid = l + r >> 1;
if (tree[rt << 1].sum >= len)return query(len, l, mid, rt << 1);
if (tree[rt << 1].rmaxn + tree[rt << 1 | 1].lmaxn >= len)return mid - tree[rt << 1].rmaxn + 1;
return query(len, mid + 1, r, rt << 1 | 1);
}
struct Node {
ll sta, end;
};
vector<Node>rem;
bool judge(int mid, int num)
{
return rem[mid].sta <= num;
}
int find(int num)
{
int id = -1;
int l = 0, r = rem.size() - 1;
while (l <= r)
{
int mid = l + r >> 1;
if (judge(mid, num))
{
id = mid;
l = mid + 1;
}
else r = mid - 1;
}
return id;
}
void solve(int n, int m)
{
rem.clear();
memset(tree, 0, sizeof(tree));
creat(1, n, 1);
while (m--)
{
char mark[10];
scanf("%s", mark);
if (mark[0] == 'N')
{
int num = read;
if (tree[1].sum < num)
puts("Reject New");
else
{
ll sta = query(num, 1, n, 1);
update(sta, sta + num - 1, 1, 1, n, 1);
printf("New at %d\n", sta);
int id = find(sta) + 1;
rem.insert(rem.begin() + id, Node{
sta,sta + num - 1 });
}
}
else if (mark[0] == 'R')
{
update(1, n, 2, 1, n, 1);
rem.clear();
puts("Reset Now");
}
else if (mark[0] == 'F')
{
int num = read;
int id = find(num);
if (id == -1 || !(rem[id].sta <= num && rem[id].end >= num))
puts("Reject Free");
else
{
printf("Free from %d to %d\n", rem[id].sta, rem[id].end);
update(rem[id].sta, rem[id].end, 2, 1, n, 1);
rem.erase(rem.begin() + id);
}
}
else
{
int num = read;
if (num > rem.size())
puts("Reject Get");
else
printf("Get at %d\n", rem[num - 1].sta);
}
}
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
solve(n, m);
puts("");
}
}
By-Round Moon
版权声明
本文为[Round moon]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_35339563/article/details/119819880
边栏推荐
- Introduction to nonparametric camera distortion model
- copy constructor
- 相机标定:关键点法 vs 直接法
- 2022LDU寒假训练-程序补丁
- Running QT program in visual Stdio
- 【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
- Make your own small program
- Installation of GCC, G + +, GDB
- undefined reference to `Nabo::NearestNeighbourSearch
- [ThreadX] h743 + ThreadX + Filex migration record
猜你喜欢

Opencv uses genericindex for KNN search

进程管理命令

Dynamic creation and release, assignment and replication of objects

文件查看命令和用户管理命令

CUDA环境安装
![[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)](/img/98/becd691d3d46f74f7666f5cb323eaf.png)
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)

Shell脚本 &&和||的使用

CUDA project encountered a series of compilation problems after changing the environment (computer)

For() loop parameter call order
![[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit](/img/4f/7ca6505b545fb825b0dba36f474da7.png)
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
随机推荐
卷积神经网络实现CIFAR100数据集分类
gcc ,g++,gdb的安装
[ThreadX] ThreadX source code reading plan (I)
生成快捷方式
Collection of practical tips for C language (continuously updated)
undefined reference to `Nabo::NearestNeighbourSearch
CUDA environment installation
对象的动态建立和释放,赋值和复制
信息学一本通-小球
Notes on advanced points of C language 4
[opencv] use filestorage to read and write eigenvectors
Static member
类的继承与派生
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
【UDS统一诊断服务】四、诊断典型服务(4)— 在线编程功能单元(0x34-0x38)
【学习一下】HF-Net 训练
PN结、二极管原理详解与应用
PM2 deploy nuxt related commands
基于VGG对五种类别图片的迁移学习