hdu--4974--貪心

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

多校的題 切了5 6題 就這道會點= =

其他都是些什麼樹的分治 圖論的一些東西= =

這題 貪心 不難發現吧 每次都盡量選取各自都能獲得1分的方案 但可能會出現1 0這種只能一個人得1分 一個人得0分得的方案。

一開始 我很sb地 wa在任意選取了2人 比較他們得分大小。。

正確姿勢當然是要 從大到小的來選取。  然後剩餘的繼續留著選取

放入個優先隊列來實現比較好

 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6  7 priority_queue<int> q; 8  9 void init( )10 {11     while( !q.empty() )12         q.pop();13 }14 15 int main()16 {17     cin.sync_with_stdio(false);18     int t , n , num , ans;19     int x , y;20     cin >> t;21     for( int k = 1 ; k<=t ; k++ )22     {23         cin >> n;24         init( );25         ans = 0;26         for( int i = 1 ; i<=n ; i++ )27         {28             cin >> num;29             q.push(num);30         }31         while( !q.empty() )32         {33             x = q.top();34             if( x<=0 )35                 break;36             q.pop();37             if( !q.empty() )38             {39                 y = q.top();40                 q.pop();41                 if( y<=0 )42                 {43                     ans += x;44                     break;45                 }46                 q.push( x-y );47                 ans += y;48             }49             else50             {51                 ans += x;52                 break;53             }54         }55         cout << "Case #" << k << ": " << ans << endl;56     }57     return 0;58 }    
View Code

 

hdu--4974--貪心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.