天梯赛_L2-046 天梯赛的赛场安排(模拟)

团体程序设计天梯赛

L2-046 天梯赛的赛场安排

模拟。注意数组大小。

#include <bits/stdc++.h>
using namespace std;

int n, c;
const int N = 5010;
struct sch{
    int id;
    string name;
    int pcnt;
    int saichang;
    bool operator < (const sch &t) const{
        return pcnt < t.pcnt;
    }
} schs[N];    // 学校信息
priority_queue<sch> h;
int sc[N * 510];    // 赛场的空位

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> c;
    for(int i = 1; i <= n * 510; i ++) sc[i] = c;
    for(int i = 1; i <= n; i ++){
        schs[i].id = i;
        cin >> schs[i].name >> schs[i].pcnt;
        h.push(schs[i]);
    }
    int cnt = 0;
    while(!h.empty()){
        sch t = h.top();
        if(t.pcnt >= c){
            sc[++ cnt] = 0;
            schs[t.id].saichang ++;
            t.pcnt -= c;
            h.pop();
            if(t.pcnt > 0) h.push(t);
        }
        else{
            bool flag = false;
            for(int i = 1; i <= cnt; i ++){
                if(sc[i] >= t.pcnt){
                    sc[i] -= t.pcnt;
                    schs[t.id].saichang ++;
                    t.pcnt = 0;
                    h.pop();
                    flag = true;
                    break;
                }
            }
            if(!flag){    // 没找到
                sc[++ cnt] -= t.pcnt;
                schs[t.id].saichang ++;
                t.pcnt = 0;
                h.pop();
            }
        }
    }
    for(int i = 1; i <= n; i ++) cout << schs[i].name << ' ' << schs[i].saichang << '\n';
    cout << cnt << '\n';
    return 0;
}