• <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>

            我希望你是我獨家記憶

            一段永遠封存的記憶,隨風而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

            PKU——2442——(堆的題目)

            Posted on 2008-08-28 15:38 Hero 閱讀(580) 評論(1)  編輯 收藏 引用 所屬分類: 代碼如詩--ACM
            //PKU 2442 Accepted 260K 438MS C++ 2837B 

            //兩個隊列--每次只處理兩行

            //這個題目讓我對"堆"有了新的認識--雙向映射

            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>

            const int INF = 100000000 ;
            const int size = 2010 ;
            int data1[size] ;
            int point[size] ;//data1[]的每個元素的指針
            int data2[size] ;
            int data3[size] ;//保存data1[]和data2[]相加后的最小inn個元素

            struct QUE
            {
                
            int len ;
                
            int toheap ;
            };
            struct QUE que[size] ;
            //暫存隊列--求出data1[]+data2[]和最小的前n個元素--data3-->data1
            int cque ;
            int heap[size] ;//指向data3[]
            int cheap ;

            int innum ; int ct ;
            int inn, inm ;

            int cmp( const void *a, const void *b )
            {
                
            return *(int *)a - *(int *)b ;
            }

            void input()
            {
                scanf( 
            "%d %d"&inm, &inn ) ;
                
            forint i=1; i<=inn; i++ )
                {
                    scanf( 
            "%d"&data1[i] ) ; point[i] = 1 ; 
                }
                qsort( data1
            +1, inn, sizeof(data1[1]), cmp ) ;

                data1[inn
            +1= INF ;
            }


            void swap( int &a,int &b )
            {
                
            int t = a ; a = b ; b = t ;
            }

            void moveup( struct QUE *dist, int n )
            {
            //將新加入的點向上移動來維持堆,n表示要向上移動的點的坐標

                
            //    while( n&&arry[n]>arry[(n-1)/2] )
                while( n&&dist[heap[n]].len<dist[heap[(n-1)/2]].len )
                {
                    swap( dist[heap[n]].toheap,dist[heap[(n
            -1)/2]].toheap ) ;
                    swap( heap[n],heap[(n
            -1)/2] ) ;
                    n 
            = ( n-1 ) / 2 ;
                }
            }


            void movedown( struct QUE *dist, int n )
            {
            //堆頂改變后,將其向下移動維持堆.n表示堆中元素總數(shù)目

                
            int i = 0 ;
                
            while( i+i+1 < n )
                {
                    
            //if( i+i+2<n&&arry[i+i+2]>arry[i+i+1]&&arry[i+i+2]>arry[i] )
                    if( i+i+2<n&&dist[heap[i+i+2]].len<dist[heap[i+i+1]].len&&dist[heap[i+i+2]].len<dist[heap[i]].len )
                    {
                        swap( dist[heap[i
            +i+2]].toheap, dist[heap[i]].toheap ) ;
                        swap( heap[i
            +i+2],heap[i] ) ;
                        i 
            = i+i+2 ;
                    }
                    
            //else if( arry[i+i+1] > arry[i] ) 
                    else if( dist[heap[i+i+1]].len < dist[heap[i]].len )
                    {
                        swap( dist[heap[i
            +i+1]].toheap, dist[heap[i]].toheap ) ;
                        swap( heap[i
            +i+1],heap[i] ) ;
                        i 
            = i+i+1 ;
                    }
                    
            else    break ;
                }
            }

            void process()
            {
                
            forint i=2; i<=inm; i++ )
                {
                    
            forint j=1; j<=inn; j++ )    
                    {
                        scanf( 
            "%d"&data2[j] ) ; 
                    }
            //下一行數(shù)據(jù)輸入--求兩行和最小的前n個元素
                    qsort( data2+1, inn, sizeof(data2[1]), cmp ) ;

                    cheap 
            = -1 ; data2[inn+1= INF ;//為了避免point[]指針越界
                    forint k=1; k<=inn; k++ )
                    {
                        que[k].len 
            = data1[k] + data2[1] ; point[k] = 1 ;
                        heap[
            ++cheap] = k ; que[k].toheap = cheap ;//先heap再toheap
                    }
                    
            int temp ;
                    
            forint k=1; k<=inn; k++ )
                    {
                        data3[k] 
            = que[heap[0]].len ; //彈出堆頂?shù)脑?/span>
                        point[heap[0]]++ ; //printf( "point[%d]==%d\n", heap[0], point[heap[0]] ) ;
                        temp = data1[heap[0]]+data2[point[heap[0]]] ;
                        que[heap[
            0]].len = temp ;//更新que[]隊列的值
                        movedown( que, cheap+1 ) ;
                    }
            //找出和最小的inn個元素

                    
            //memcpy( data1, data3, inn+1 ) ;
                    forint k=1; k<=inn+1; k++ ) data1[k] = data3[k] ;
                }
            }

            void output() 
            {
                
            forint i=1; i<inn; i++ ) printf( "%d ", data1[i] ) ;

                printf( 
            "%d\n", data1[inn] ) ;
            }

            int main()
            {
                
            while( scanf( "%d"&innum ) != EOF )
                {
                    
            forint ct=1; ct<=innum; ct++ )
                    {
                        input() ;

                        process() ;

                        output() ;
                    }
                }

                
            return 0 ;
            }

            Feedback

            # re: PKU——2442——(堆的題目)  回復  更多評論   

            2011-03-15 21:33 by acmer
            謝謝,我用了你的方法過了這道題目!
            可以加你QQ嗎?
            我的QQ是339379406
            无码人妻精品一区二区三区久久久 | 蜜桃麻豆www久久国产精品| 丁香色欲久久久久久综合网| 久久久久国色AV免费看图片| 91久久九九无码成人网站| 色综合久久久久网| 亚洲国产成人久久精品影视 | 久久久精品午夜免费不卡| 漂亮人妻被黑人久久精品| 久久精品www人人爽人人| 久久精品人人做人人爽电影蜜月| 久久久久高潮综合影院| 人妻精品久久无码专区精东影业| 97久久婷婷五月综合色d啪蜜芽| 久久久久久精品无码人妻| 亚洲AV日韩AV天堂久久| 国产亚洲精品自在久久| 久久亚洲高清观看| 亚洲一区精品伊人久久伊人| 蜜桃麻豆WWW久久囤产精品| 亚洲AV无一区二区三区久久| 久久久久久国产精品无码超碰| 久久99热国产这有精品| 无码任你躁久久久久久老妇| 久久久久se色偷偷亚洲精品av| 久久99久久99精品免视看动漫| 国产免费久久精品99久久| 亚洲欧美精品一区久久中文字幕| 伊人久久综合精品无码AV专区| 亚洲国产精品久久66| 大香伊人久久精品一区二区| AV狠狠色丁香婷婷综合久久| 美女久久久久久| 久久99中文字幕久久| 午夜精品久久久久久影视riav| 99久久精品国产免看国产一区| 久久综合五月丁香久久激情| 99久久国产热无码精品免费| 综合久久给合久久狠狠狠97色| 久久伊人精品青青草原高清| 亚洲精品成人网久久久久久|