标签:
题目大意:给你一段内存,要你进行如下的三个操作。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; const int INF = 1e9+7; const int maxn = 1055; int Arr[maxn]; int n, m, a, ans; int Blocks = 0; char op[50]; int Alloc(int a) { for(int i=1; i<=m; i++) { int j = i, num = 0; while(Arr[j] == 0 && num != a && j <= m) num ++, j ++; if(num != a) i = j; else { Blocks ++; for(int k=i; k<j; k++) Arr[k] = Blocks; return Blocks; } } return 0; } int Erase(int Id) { if(Id <= 0) return -1; bool flag = false; for(int i=1; i<=m; i++) { while(Arr[i] == Id) Arr[i++] = 0, flag = true; } if(flag) return -2; return -1; } int Defragment() { for(int i=1; i<=m; i++) { if(Arr[i] == 0) { for(int j=i+1; j<=m; j++) { if(Arr[j]) { swap(Arr[i], Arr[j]); break; } } } } return -5; } int main() { scanf("%d %d", &n, &m); memset(Arr, 0, sizeof(Arr)); while(n --) { scanf("%s", op); if(strcmp(op, "alloc") == 0) { scanf("%d", &a); ans = Alloc(a); } else if( strcmp(op, "erase") == 0 ) { scanf("%d", &a); ans = Erase(a); } else ans = Defragment(); if( ans == 0) puts("NULL"); else if(ans == -1) puts("ILLEGAL_ERASE_ARGUMENT"); else if(ans >= 1) printf("%d\n", ans); } return 0; }
CodeForce 7 B - Memory Manager(模拟)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4905112.html