CodeForce 7 BメモリManager(中難易度シミュレーション)

6669 ワード

1、http://codeforces.com/problemset/problem/7/B
2、これは模擬問題です。細かいところが多くて、注意すれば正しいです。
タイトル:
B.メモリManager
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
out put
スタンダードアウト
The is little time left before the release of the first national operate system BerlOS.Some of its components are not finished yet-the memory manager is among the m.According to the developers'the the firemarse
  • alloc n-to allocate n bytes of the memory and return the allocated block's identifer x;
  • erase x—to erase the block with the identifer x;
  • defragment-to defragment the free memory,briging all the blocks as close to the beginning of the memoryas possible and preserving their respive order;
  • The memory model in this case is very simple.It is a sequence of m bytes,numberd for conventience from the first to the m-th.
    The first operation alloc n Tas s s s s the onlyparameterthe sizs of the memory block ththaat is to to lolocated.While processing thisoperation,a free block of n succeive bytes isisisbealallocated the memomomoriblbleeeeeeeeeeblblblblblaaaaaaaaaaaathethethethethethe blblblbleeeeeeeeeeblblblblblblbleeeeeeeeeeeeblblblblblblblblblbleeeeeeeeeeeeeeeeeet byte)is prefered.All these bytes are marked as not free,and the memory manager returns a 32-bit integer numerical token that is the identifer of this block.If it is impossible to allocate a frete a free block of therence。
    The second operation erase e x tas s s parameter the dentifier of some block.Thisoperation frerees the sysstem memorymaking the bytes s s s s of the frerereree for furtheuse.Inthe case case wheheshshshaathisisisisisisisisthethethethethetherererererereinininininininininininininininststststststststststrerererererererererererererererererererererererererererererereeeeeeeeeeeeeeeeeeeeeeeL_。ERASE_ARGUMENT.
    The last operation defragment does not have any argments and simply brigs the occupied memory sections closer to the begining of the memory without change the res their pective order.
    In the current implemention you are success ive integers,starting with 1,as identifers.Each success ful alloc operation procession shored return following number.Uniccess ful alloc operation.noffect.
    You are to write the implemention of the memory manager.You Shoutput the returned value for each alloc command You shout ILLEGALERASE_ARGUMENT for all the failed erase commands.
    Input
    The first line of the input data contains two positive integers t and m(1̵≦?t≤?100;1?m≦?̵̵̵̵100)where-the amoution ofand m-the available memory size in bytes.The n there follw t lins where the operatis themselves themselves arveveves.The first opation is alloc n(1?≤?nn n?n≦≦≦≦?nn n n≦≦≦≦≦≦≦?100)))))),heheherererereeeeeeinininininininininininininininininininininininininininininininininininininininininininininininininininininininIS defragment.
    Output
    Output the sequence of lineas.Each line shot contain eigher the rerrout of alloc operation procession,or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation.Output lineas shoruld go in the same order in in which the operation.Success procession of alloc operation shoperation Shound integers,starting the blade 1
    Sample test(s)
    Input
    6 10
    alloc 5
    alloc 3
    erase 1
    alloc 6
    defragment
    alloc 6
    
    Output
    1
    2
    NULL
    3
    
    3、ACコード:
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    char s[25];
    int a[1005];
    int b[1005];
    int main()
    {
        int t,m,n,num;
        while(scanf("%d%d",&t,&m)!=EOF)
        {
            num=1;
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            while(t--)
            {
                scanf("%s",s);
                if(s[0]=='a')
                {
                    scanf("%d",&n);
                    int flag=0;
                    for(int i=1; i<=m; i++)
                    {
                        if(flag==1)
                            break;
                        if(a[i]==0)
                        {
                            int cnt=1;
                            if(cnt==n)
                            {
                                a[i]=num;
                                b[num]=1;
                                printf("%d
    ",num); num++; flag=1; } for(int j=i+1; j<=m; j++) { if(a[j]==0) { cnt++; //printf("*%d %d
    ",j,cnt); if(cnt==n) { //printf("ij=%d %d
    ",i,j); for(int k=i; k<=j; k++) a[k]=num; b[num]=1; printf("%d
    ",num); num++; flag=1; break; } } else break; } } } if(flag==0) printf("NULL
    "); } else if(s[0]=='e') { scanf("%d",&n); if(n<=0 || n>=num || b[n]==0) { printf("ILLEGAL_ERASE_ARGUMENT
    "); } else { for(int i=1; i<=m; i++) { if(a[i]==n) a[i]=0; } b[n]=0; } } else if(s[0]=='d') { int c[1005]; memset(c,0,sizeof(c)); int k=1; for(int i=1; i<=m; i++) { if(a[i]!=0) { c[k++]=a[i]; a[i]=0; } } for(int i=1; i<=k; i++) { a[i]=c[i]; } } } } return 0; } /* 3 10 alloc 11 alloc 3 erase 3 alloc 5 defragment alloc 6 3 10 a 10 a 2 a 3 3 1 a 3 a 1 a 2 3 1 e -1 a 1 e 1 */