#37365: C++


shane950517@gmail.com (ㄟ不是)

學校 : 國立臺中高級工業職業學校
編號 : 166971
來源 : [60.249.14.165]
最後登入時間 :
2024-02-28 10:14:56
j608. 4. 機器出租 -- 2023年1月APCS | From: [122.118.23.194] | 發表日期 : 2023-09-04 03:28

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
 
int cmp(pair<int, int> a, pair<int, int> b) {
    if (a.second == b.second) return a.first < b.first;
    else return a.second < b.second;
}
 
int main() {
    
ios_base::sync_with_stdio(false);
    cin.tie(0);   
 
int num_event, num_pc;
    cin >> num_event >> num_pc;
    vector<pair<int, int>> event(num_event);
    for (int i = 0; i < num_event; i++) cin >> event[i].first;
    for (int i = 0; i < num_event; i++) cin >> event[i].second;
    sort(event.begin(), event.end(), cmp);
 
    int ans = 0, which_pc_max = -10;
    vector<int> pc_busy(num_pc, -1);
 
    for (int i = 0; i < num_event; i++) {
        for (int j = 0; j < num_pc; j++) {
            if (pc_busy[j] < event[i].first && which_pc_max == -10) {
                which_pc_max = j;
            }
            else if (pc_busy[j] < event[i].first && pc_busy[j] >= pc_busy[which_pc_max]) {
                which_pc_max = j;
            }
        }
        if (which_pc_max != -10) {
            pc_busy[which_pc_max] = event[i].second;
            ans++;
            which_pc_max = -10;
        }
    }
 
    cout << ans << '\n';
 
    return 0;
}

 

 
ZeroJudge Forum