[codeforces] 854C. Planning(优先队列)

来源:互联网 发布:centos nat 配置 编辑:程序博客网 时间:2024/06/11 09:13

[codeforces] 854C. Planning(优先队列)


题目链接:
C. Planning


题目大意:
n个航班要按顺序依次起飞, 现在要推迟k秒, 每个飞机有一个停留花费ci每秒, 你可以安排这些飞机的起飞顺序, 但前提是不能早于原始起飞时间, 求最小花费。

数据范围:
(1kn300000)
(1ci107)
解题思路:

对于任意两个飞机a, b如果后者的花费大于前者, 那么两个交换位置肯定更优, b再跟前面的比往前比较, 直到不再比前面的飞机花费大了或者b到了原来的起飞时间(不能更早了)。

有了这个思路之后, 首先能在第一秒起飞的是1到k + 1号飞机, 因为k + 2号飞机最早最早也只能第二秒起飞。 这样我们可以用优先队列维护k + 1个飞机, 每次起飞(弹出)最大的, 然后再放入下一个飞机。

代码:

/******************************************** *Author*        :ZZZZone *Created Time*  : 四  9/ 7 12:56:25 2017 *Ended  Time*  : 四  9/ 7 13:17:00 2017*********************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<queue>using namespace std;const int MaxN = 3e5;typedef long long LL;typedef pair<int, int> PII;int ans[MaxN + 5], c[MaxN + 5];priority_queue<PII>q;int n, k;int main(){    while(~scanf("%d %d", &n, &k)){        for(int i = 1; i <= n; i++){            scanf("%d", &c[i]);        }        for(int i = 1; i <= k + 1; i++) q.push(make_pair(c[i], i));        LL tot = 0;        int xx = k + 1;        for(int i = k + 2; i <= n; i++){            int now = q.top().second;            ans[now] = xx++;            tot += 1LL * (ans[now] - now) * c[now];            q.pop();            q.push(make_pair(c[i], i));        }        while(!q.empty()){            int now = q.top().second;            ans[now] =  xx++;            tot += 1LL * (ans[now] - now) * c[now];            q.pop();        }        printf("%lld\n", tot);        for(int i = 1; i <= n; i++) printf("%d ", ans[i]);    }    return 0;}

标签:codeforces 优先队列

原创粉丝点击