【題解】1072 . A.誰先晚餐

Judge:https://tioj.ck.tp.edu.tw/problems/1072

題目

有n個人要吃晚餐,給定 cook time、eat time,請你決定上菜順序,求最短的總時間。

解法

  • 先上菜給:吃最慢的人。
  • 看有沒有人煮完的時候還沒吃完。

程式碼

#include <iostream>
#include <queue>
using namespace std;
int main(){
  int n, c, e, ct, ans;
  priority_queue<pair<int,int>> pq;
  while(cin >> n&&n){
    ct=0, ans=0;
    for(int i=0; i<n; i++){
      cin >> c >> e;
      pq.push(make_pair(e,c));
    }
    while(!pq.empty()){
      ct += pq.top().second;
      ans = max(ans, ct+pq.top().first);
      pq.pop();
    }
    cout << ans << '\n';
  }
}
發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *