贪心的思想
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 5 using namespace std; 6 7 int main(){ 8 int n,m; 9 while(cin >> n>>m && n && m){ 10 vector<int> heads(n),knights(m); 11 for(int i = 0; i < n; i ++ ) cin >> heads[i]; 12 for(int i = 0; i < m; i ++ ) cin >> knights[i]; 13 sort(heads.begin(),heads.end()); 14 sort(knights.begin(),knights.end()); 15 int minGold = 0; 16 int i,j = 0; 17 for(i = 0; i < n && j < m; i ++){ 18 for(; j < m ; j ++ ) 19 if(knights[j] >= heads[i]) break; 20 minGold +=knights[j++]; 21 } 22 if(i < n) cout<<"Loowater is doomed!"<<endl; 23 else cout<<minGold<<endl; 24 } 25 return 0; 26 }