PTA 7-1 银行排队问题之单队列多窗口服务(25 分)25分代码 (小模拟)

银行排队问题

直接模拟就好,错了一次,没看见超过60分的按60算



#include

using namespace std;

const int maxn = 1000 + 7, INF = 0x7f7f7f7f;
int n, k;
int b[11] = {0};
int cnt[11] = {0};
struct node {
    int t, p;
    int len;
}a[maxn];

void init() {
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d %d", &a[i].t, &a[i].p);
        if(a[i].p > 60) a[i].p = 60;
    }
    scanf("%d", &k);
}

void solve() {
    for(int i = 0; i < n; ++i) {
        int id = 1;
        for(int j = 1; j <= k; ++j) {
            if(b[id] > b[j]) id = j;
            if(a[i].t >= b[id]) break;
        }
        if(a[i].t >= b[id]) {
            b[id] = a[i].t + a[i].p;
            a[i].len = 0;
            cnt[id]++;
        }
        else {
            a[i].len = b[id] - a[i].t;
            b[id] = b[id] + a[i].p;
            cnt[id]++;
        }
        //cout << id << " === " << endl;
        //cout << b[1] << " " << b[2] << " " << b[3] << endl;
    }
    double ans1 = 0;
    int ans2 = 0, ans3 = 0;
    for(int i = 1; i <= k; ++i) {
        ans3 = max(ans3, b[i]);
    }
    int sum = 0;
    for(int i = 0; i < n; ++i) {
        ans2 = max(ans2, a[i].len);
        sum += a[i].len;
    }
    ans1 = (sum*1.0 / n*1.0);
    printf("%.1lf %d %d\n", ans1, ans2, ans3);
    for(int i = 1; i <= k; ++i)
        printf("%d%c", cnt[i], (i == k ? '\n' : ' '));
}

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


你可能感兴趣的:(PTA)