• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜


            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋    

             

            題目地址:

            http://acm.hdu.edu.cn/showproblem.php?pid=2871

            題目描述:

            Memory Control

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
            Total Submission(s): 703    Accepted Submission(s): 147


            Problem Description
            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:
            1.  Reset Reset all memory units free.
            2.  New x Allocate a memory block consisted of x continuous free memory units with the least start number
            3.  Free x Release the memory block which includes unit x
            4.  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
            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.
             

            Output
            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
             

            題目分析 :

            PKU3667 HOTEL的加強(qiáng)版,而且 HOTEL 3個(gè)函數(shù)都沒(méi)有任何變化。

              第一哥指令 New  就是找一段長(zhǎng)為X的最左邊的區(qū)間,這個(gè)和HOTEL是沒(méi)有區(qū)別,還是用那三個(gè)函數(shù)找到最左邊的區(qū)間并加以更新,cov = 1

              第二個(gè)指令是Free  x就是找一個(gè)包含X的區(qū)間,咋一看以為要重寫一個(gè)QUERY函數(shù),其實(shí)沒(méi)有必要,使用VECTOR數(shù)組保存下每次占領(lǐng)的區(qū)間,并且由于VECTOR是向量,可以刪除

            中間的,并任意在中間加區(qū)間,十分方便。那我們現(xiàn)在向量里找到那個(gè)區(qū)間,接著進(jìn)行更新,cov = 0;

              第三個(gè)指令是Get x就是找到第X個(gè)區(qū)間的范圍,用VECTOR的記錄很快就能找到

              第四個(gè)指令Reset,很方面,更新一下即可

             

            注意 二分不要寫錯(cuò)了 ,  剛開(kāi)始的 時(shí)候用的 lower_bound  WA 到我想吐.................................    具體看代碼注釋

              今天一早起來(lái) 繼續(xù)查錯(cuò).......   一直很奇怪 為什么用 lower_bound  和 upper_bound 是 WA 的.   可能是早晨頭腦比較清醒, 半個(gè)小時(shí), 終于找到了錯(cuò)誤的原因 !

            其實(shí)就是一個(gè)小小的錯(cuò)誤.....  : modify ( it->beg, it->end, 0 );

                                                          vec.erase ( it ); 

            我寫成了 這樣 :    vec.erase ( it ); 

                                                 modify ( it->beg, it->end, 0 ); 

            竟然把迭代器刪除了再 使用  ....        杯具.........................

            代碼如下 :

            /*

            Mail to   : miyubai@gamil.com

            Link      : http://www.cnblogs.com/MiYu  || http://m.shnenglu.com/MiYu

            Author By : MiYu

            Test      : 2

            Complier  : g++ mingw32-3.4.2

            Program   : HDU_2871

            Doc Name  : Memory Control

            */

            //#pragma warning( disable:4789 )

            #include <iostream>

            #include <fstream>

            #include <sstream>

            #include <algorithm>

            #include <string>

            #include <set>

            #include <map>

            #include <utility>

            #include <queue>

            #include <stack>

            #include <list>

            #include <vector>

            #include <cstdio>

            #include <cstdlib>

            #include <cstring>

            #include <cmath>

            #include <ctime>

            using namespace std;

            inline int max ( int a, int b ) {

                   return a > b ? a : b;       

            }

            struct segTree {

                   int left, right, lVal, rVal, mVal, cov;// cov -- >  0: 線段為空   1: 線段為滿  -1:2種情況都有 

                   int mid () { return (left+right)>>1; }

                   int dis () { return right - left + 1; }

                   void set ( int flag ) { // 0: 線段為空   1: 線段為滿 

                        if ( flag ){

                             cov = 1;

                             lVal = rVal = mVal = 0;  

                        } else {

                             cov = 0;

                             lVal = rVal = mVal = right - left + 1;     

                        }

                   }     

            }seg[150010];

            void creat ( int left, int right, int rt = 1 ) {   // 初始化線段 

                 seg[rt].left = left;

                 seg[rt].right = right;

                 seg[rt].set (0);

                 if ( left == right ) return;

                 int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();  

                 creat ( left, mid, LL );

                 creat ( mid + 1, right, RR );  

            }

            void modify ( int left, int right, int flag, int rt = 1 ) {

                 if ( seg[rt].left >= left && seg[rt].right <= right ) {   //如果線段被覆蓋,  直接按flag標(biāo)記處理,返回 

                     seg[rt].set ( flag );   return;

                 }     

                 int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();

                 if ( seg[rt].cov != -1 ) {     // 如果線段不是混合情況(即線段是被覆蓋的), 把標(biāo)志下傳 

                      seg[LL].cov = seg[RR].cov = seg[rt].cov;

                      seg[LL].set ( seg[LL].cov );   

                      seg[RR].set ( seg[RR].cov );

                 } 

                 if ( right <= mid ) modify ( left, right, flag, LL );    //遞歸更新線段 

                 else if ( left > mid ) modify ( left, right, flag, RR );

                 else {

                       modify ( left, mid, flag, LL );

                       modify ( mid + 1, right, flag, RR );     

                 }

                 if ( seg[LL].cov == 0 && seg[RR].cov == 0 ) seg[rt].cov = 0; //線段為空 

                 else if ( seg[LL].cov == 1 && seg[RR].cov == 1 ) seg[rt].cov = 1; //線段滿 

                 else seg[rt].cov = -1;  // 2種情況都有 

                 seg[rt].mVal = max(seg[LL].rVal+seg[RR].lVal,max(seg[LL].mVal, seg[RR].mVal)); // 線段的更新,  經(jīng)典部分

                 seg[rt].lVal = seg[LL].lVal + ( seg[LL].cov == 0 ? seg[RR].lVal : 0 );

                 seg[rt].rVal = seg[RR].rVal + ( seg[RR].cov == 0 ? seg[LL].rVal : 0 );

            }

            int query ( int val, int rt = 1 ) {

                int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();

                if ( seg[rt].cov == 0 && seg[rt].mVal >= val ) {   //線段為空,且可用,直接返回線段左端點(diǎn) 

                         return seg[rt].left;

                } else if ( seg[rt].cov == -1 ) {   //分三種 情況處理  左   左右    右  處理   

                     if ( seg[LL].mVal >= val ) return query ( val, LL );

                     else if ( seg[LL].rVal + seg[RR].lVal >= val ) return mid - seg[LL].rVal + 1;

                     else if ( seg[RR].mVal >= val )return query ( val, RR );  

                }   

                return 0;

            }

            struct P {

                   int beg, end;

                   void set(int &a, int b) { beg = a; end = b; }

                   friend bool operator < ( const P& a, const P& b ) {

                        return a.beg < b.beg;     

                   }       

            };

            vector <P> vec;

            vector <P>::iterator it;

            int find ( int &x ) {   // 2分找滿足要求的線段的下一條線段 

               int beg = 0;

               int end = vec.size() - 1;

               while ( beg <= end ) {

                     int mid = ( beg + end ) >> 1;

                     if ( vec[mid].beg <= x ) {

                        beg = mid + 1;

                       } else {

                            end = mid - 1;

                       }

                }

               return beg;

            }

            inline bool scan_ud(int &num)  

            {

                    char in;

                    in=getchar();

                    if(in==EOF) return false;

                    while(in<'0'||in>'9') in=getchar();

                    num=in-'0';

                    while(in=getchar(),in>='0'&&in<='9'){

                            num*=10,num+=in-'0';

                    }

                    return true;

            }

            int main ()

            {

                int N, M, left, right, val, choice;

                char ask[10];

                P temp, tt;

                while ( scan_ud(N)&& scan_ud(M) ) {

                       creat ( 1, N );

                       vec.clear();

                       while ( M -- ) {

                             scanf ( "%s",ask );

                             switch ( ask[0] ) {

                                     case 'R' : modify ( 1, N, 0 );  //因?yàn)榫€段已經(jīng)構(gòu)建好了, 更新下就可以了 

                                                vec.clear();     //記得 vec clear() 一下 

                                                puts ( "Reset Now" );

                                                break;

                                     case 'N' : scan_ud( val );

                                                left = query ( val );  // 和 PKU3667 沒(méi)區(qū)別 

                                                if ( left == 0 || seg[1].mVal < val ) puts ( "Reject New" );        

                                                else {

                                                      modify ( left, left + val - 1 , 1 );

                                                      temp.set( left,left+val-1 );

                                                      right = find ( left );

                                                      vec.insert ( vec.begin() + right, temp );

                                                      printf ( "New at %d\n",left );     

                                                }   

                                                break;

                                     case 'F' : scan_ud( val );

                                                right = find ( val ) - 1;

                                                if ( right == -1 || vec[right].end < val ) { //沒(méi)找到 

                                                 puts("Reject Free");

                                             } else {

                                                   printf ( "Free from %d to %d\n", vec[right].beg, vec[right].end );

                                                   modify ( vec[right].beg, vec[right].end, 0 );

                                                   vec.erase ( vec.begin() + right );

                                             }

                                                break;

                                     case 'G' : scan_ud( val );

                                                if ( val > vec.size() )          // 直接輸出就行 

                                                     puts ( "Reject Get" ); 

                                                else {

                                                      printf ( "Get at %d\n",vec[val-1].beg );     

                                                }   

                             }      

                       }  

                       putchar ( 10 );    

                }

                return 0;

            }

             

             

             付 STL  的 主函數(shù), 其他的 和上面的 都一樣  :

             

            代碼
            int main ()
            {
                
            int N, M, left, right, val, choice;
                
            char ask[10];
                P temp;
                
            while ( scan_ud(N)&& scan_ud(M) ) {
                       creat ( 
            1, N );
                       vec.clear();
                       
            while ( M -- ) {
                             scanf ( 
            "%s",ask );
                             
            switch ( ask[0] ) {
                                     
            case 'R' : modify ( 1, N, 0 );
                                                vec.clear();
                                                puts ( 
            "Reset Now" );
                                                
            break;
                                     
            case 'N' : scan_ud( val );
                                                left 
            = query ( val );
                                                
            if ( left == 0 || seg[1].mVal < val ) puts ( "Reject New" );        
                                                
            else {
                                                      modify ( left, left 
            + val - 1 , 1 );
                                                      temp.set( left,left
            +val-1 );
                                                      it 
            = lower_bound ( vec.begin(),vec.end(),temp );
                                                      vec.insert ( it, temp );
                                                      printf ( 
            "New at %d\n",left );     
                                                }   
                                                
            break;
                                     
            case 'F' : scan_ud( val );
                                                temp.set ( val,val );
                                                it 
            = upper_bound ( vec.begin(),vec.end(),temp );
                                                
            if ( vec.empty() || it == vec.begin() || (it-1)->end < val || (it-1)->beg > val ) {
                                                     puts ( 
            "Reject Free" );
                                                     
            break;
                                                }
                                                it 
            --;
                                                printf ( 
            "Free from %d to %d\n",it->beg, it->end );
                                                modify ( it
            ->beg, it->end, 0 );
                                                vec.erase ( it );    
                                                
            break;
                                     
            case 'G' : scan_ud( val );
                                                
            if ( val > vec.size() )
                                                     puts ( 
            "Reject Get" ); 
                                                
            else {
                                                      printf ( 
            "Get at %d\n",vec[val-1].beg );     
                                                }   
                             }      
                       }  
                       putchar ( 
            10 );    
                }
                
            return 0;
            }

             

             

            Feedback

            # re: HDU 2871 HDOJ 2871 Memory Control ACM 2871 IN HDU[未登錄](méi)  回復(fù)  更多評(píng)論   

            2010-10-13 14:28 by 小杰
            第一次來(lái),沙發(fā)了~~

            # re: HDU 2871 HDOJ 2871 Memory Control ACM 2871 IN HDU[未登錄](méi)  回復(fù)  更多評(píng)論   

            2011-04-01 22:10 by dd
            我很喜歡你寫的代碼
            久久久久人妻一区二区三区 | 国内精品久久久久久久久| 超级97碰碰碰碰久久久久最新| 超级碰久久免费公开视频| 国产精品久久久久久影院 | 婷婷综合久久中文字幕| 久久精品水蜜桃av综合天堂| 97精品依人久久久大香线蕉97| 久久精品人妻一区二区三区| 好久久免费视频高清| 日本免费一区二区久久人人澡| 久久精品男人影院| 久久99精品久久久久久野外 | 久久国产精品免费一区| 国产精品九九久久免费视频 | 久久综合九色综合欧美狠狠| 伊人久久大香线蕉精品| 亚洲一区中文字幕久久| 久久精品中文字幕有码| 亚洲综合久久夜AV | 亚洲精品无码久久久久久| 国产精品久久成人影院| 国产午夜精品久久久久九九| 久久久久久国产精品美女| 久久人人爽人人爽人人片AV麻烦| 久久婷婷五月综合色奶水99啪| 国产成人精品免费久久久久| 国产午夜福利精品久久| 国产激情久久久久久熟女老人| 72种姿势欧美久久久久大黄蕉| 99久久国产亚洲高清观看2024 | 久久93精品国产91久久综合| 久久天天躁夜夜躁狠狠躁2022| 国产午夜久久影院| 亚洲人成电影网站久久| 国产精品久久自在自线观看| 午夜精品久久久久9999高清| 久久久一本精品99久久精品66| 久久久久九国产精品| 国产91色综合久久免费| 久久亚洲AV无码精品色午夜麻豆|