• CodeForce 7 B


    题目大意:给你一段内存,要你进行如下的三个操作。

    1.分配内存  alloc   X ,分配连续一段长度为X的内存。
    如果内存不够应该输出NULL,如果内存够就给这段内存标记一个编号。
    2.擦除编号为 X的内存,erase X,  如果这段内存不存在那么输出“ILLEGAL_ERASE_ARGUMENT ”,否则什么都不输出。
    3.整理内存,把所有的内存块整理到一块。
     
    #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
    ", ans);
    
        }
    
    
        return 0;
    }
  • 相关阅读:
    C# 抽象方法和虚方法的区别
    xmlhttprequest readyState 属性的五种状态
    ServiceStack破解文件
    k8s部署mysql
    docker 开放2376端口的问题
    .net core 发布到IIS 没有 web.config 文件
    1064
    docker mysql 主从同步配置
    Docker 鼠标在虚拟机与主机之间自由切换
    Socket原理解析2
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4905112.html
Copyright © 2020-2023  润新知