標籤: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--貪心