还有比我这更简单的方法吗

CF7B Memory Manager

gaomingyang101011 @ 2024-06-20 18:59:11

#include<iostream>
#include<cstring>
using namespace std;
int m,t,n;
int neicun[101],biaohao;
string a;
bool pd;
int main(){
    cin>>t>>m;
    while(t--){
        cin>>a;
        if(a!="defragment") cin>>n;
        if(a=="alloc"){
            bool pdalloc=0;
            for(int i=1;i<=m-n+1;i++){
                pd=0;
                for(int j=0;j<n;j++)
                    if(neicun[i+j]!=0){
                        pd=1;
                        break;
                    }
                if(pd==0){
                    biaohao++;
                    for(int j=0;j<n;j++) neicun[i+j]=biaohao;
                    pdalloc=1;
                    break;
                }
            }
            if(pdalloc==1) cout<<biaohao<<endl;
            else cout<<"NULL"<<endl;
        }
        else if(a=="erase"){
            bool pderase=0;
            if(n==0){
                cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;   
                continue;
            }
            for(int i=1;i<=m;i++) if(neicun[i]==n) neicun[i]=0,pderase=1;
            if(pderase==0) cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
        }   
        else{
            int kong=1;
            for(int i=1;i<=m;i++)
                if(neicun[i]!=0){
                    while(neicun[kong]!=0) kong++;
                    if(kong<=i) neicun[kong]=neicun[i],neicun[i]=0;
                }
        }
    }
    return 0;
}

|