Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C Planning

来源:互联网 发布:人工智能元年 编辑:程序博客网 时间:2024/05/29 17:35

题意  : 给你n个飞机要飞离的时刻表,然后告诉你n个飞机每延误一分钟的代价,让你安排合适的飞行方式,使得总代价最小。

题解 : 我们发现代价大的肯定要先飞走,每次选择代价花费最大的飞机飞走,这样的话我们就可以用一个堆去维护这种每次取最大的数据结构,这样的话我们先把 1 到 k 时间要飞的飞机加入堆中,然后每到一分钟就把在这一分钟要飞走的飞机加入堆中,然后从这些飞机中选择一个代价最大的飞走,这样就可可以保证开销一定是最小的。 

堆的话我们可以用 STL 中的优先队列实现就可以了。

#include <iostream>#include <algorithm>#include <cstring>#include <queue>#define  ll long longusing namespace std;const int maxn = 3e5 + 10;struct node {    int t;    int cost;    bool operator < (const node & x) const {        return x.cost > cost;    }}c[maxn];ll ans = 0;int res[maxn] = {0};int main () {    ios_base :: sync_with_stdio(false);    int n,k;    cin >> n >> k;    priority_queue<node>pq;    for (int i = 1;i <= n; ++ i) {        cin >> c[i].cost;        c[i].t = i;        if (i <= k) {            pq.push (c[i]);        }    }    for (int i = k + 1;i <= k + n; ++ i) {        if (i <= n) pq.push(c[i]);        node u = pq.top();        res[u.t] = i;        pq.pop();        ans += (ll)(i - u.t) * (ll)u.cost;    }    cout << ans << endl;    for (int i = 1;i <= n; ++ i) {        cout << res[i] << ' ';    }        return 0;}


阅读全文
0 0
原创粉丝点击