PAT甲级1017 Queueing at Bank

姥姥的银行又开业了。跟 1014 相比,简单了不少,只需要找到最早可用的窗口即可。但我还是卡了半天,读题不认真。。。PAT真就考读题。我卡了半天的点是,17:00 (含)前到的客户,无论多晚,银行都奉陪到底,这是我意想不到的。。。

#include 
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e4+4;
const int maxk = 1e2+2;
const int maxt = 9*60*60;
struct Customer {
    int arrive;
    int proc;

    Customer() {}
    Customer(int arrive, int proc): arrive(arrive), proc(proc) {}

    bool operator < (const Customer& c) const {
        return arrive < c.arrive;
    }
} customers[maxn];
int n, k, m;
int win[maxk];

int stoi(string s, int left, int right) {
    int ret = 0;
    for (int i = left; i < right; ++i) {
        ret *= 10;
        ret += s[i]-'0';
    }
    return ret;
}

int dtoi(string date) {
    int time = 0;
    time += (stoi(date, 0, 2)-8)*60*60;
    time += stoi(date, 3, 5)*60;
    time += stoi(date, 6, 8);
    return time;
}

void read() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        string date;
        int proc;
        cin >> date >> proc;
        // 只有在17:00(含)前到的客户才能拥有姓名
        if (dtoi(date) <= maxt) {
            customers[++m] = Customer(dtoi(date), min(proc*60, 3600));
        }
    }
}

void solve() {
    sort(customers+1, customers+1+m);

    int wait = 0;
    int curn = 0;
    while (++curn <= m) {
        int nextt = INF, nextw = -1;
        for (int i = 0; i < k; ++i) {
            if (nextt > win[i]) {
                nextt = win[i];
                nextw = i;
            }
        }
        
        if (customers[curn].arrive < win[nextw]) {
            // 如果已经在等了
            wait += win[nextw]-customers[curn].arrive;
            win[nextw] += customers[curn].proc;
        } else {
            // 如果来的时候已经没人了
            win[nextw] = customers[curn].arrive + customers[curn].proc;
        }
    }

    cout << fixed << setprecision(1) << wait/60.0/m << endl;
}

int main() {
    read();
    solve();
    return 0;
}

 

你可能感兴趣的:(PAT甲级题解)