Sunday, January 5, 2020

POJ.3253 Fence Repair

Problem:
http://poj.org/problem?id=3253

Idea:
Always pick smallest 2.

Source:
 int n;  
 priority_queue<int, vector<int>, greater<int>> q;  
 int main()  
 {  
      cin >> n;  
      for (int i = 0; i < n; i++) {  
           int t;  
           cin >> t;  
           q.push(t);  
      }  
      ll ans = 0;  
      for (int i = 1; i < n; i++) {  
           int t1 = q.top(); q.pop();  
           int t2 = q.top(); q.pop();  
           q.push(t1 + t2);  
           ans += (t1 + t2);  
      }  
      cout << ans << endl;  
      return 0;  
 }  

No comments:

Post a Comment