Codeforces 854C. Planning 贪心
来源:互联网 发布:sql nvl to_number 编辑:程序博客网 时间:2024/06/05 18:40
题意
给出n(3e5),k(<=n),以及n个数ci(1e7).
表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟的cost.一个飞机不能比原定时间早起飞,请安排一个起飞顺序,求最小的cost和.
解法
贪心证明
设序号为i的飞机起飞时间为di,则cost=∑(di-i)*ci=∑di*ci-∑i*ci.
显然后一项为常数,而{di-k}为[1,n]的一个排列,
所以只要使ci越大的i尽可能早起飞即可使得cost最小.
求解
对于每个[k+1,k+n]的时刻t,都会有一架飞机起飞,
而可起飞的飞机只有原定起飞时刻在[1,t]内已经准备好的飞机.从这些飞机中选取ci最大的即可.
维护一个优先队列.一次循环就可以得出结果.
注意
!!!一会再补
代码
/* LittleFall : Hello! */#include <bits/stdc++.h>using namespace std;struct item{ int idx, c; bool operator < (const item &a) const { return c < a.c; } item(int i = 0, int c = 0): idx(i), c(c) {}};priority_queue<item> pq;int tim[1000010];int main(void){ int n, k; scanf("%d%d", &n, &k); long long cost = 0; for(int i = 1; i <= n + k; i++) { int tmp; if(i <= n) { scanf("%d", &tmp); pq.push({i, tmp}); } if(i >= k + 1) { item tem = pq.top();pq.pop(); cost += 1ll * tem.c * (i - tem.idx); tim[tem.idx] = i; } } cout << cost << endl; for(int i = 1; i < n; i++) printf("%d ", tim[i] ); printf("%d\n", tim[n] ); return 0;}
阅读全文
0 0
- Codeforces 854C. Planning 贪心
- Codeforces 854 C Planning(优先队列+贪心)
- Codeforces 854C Planning【贪心+并查集】
- Codeforces Round #433 C. Planning(贪心)
- Codeforces Round #433 (Div. 2) C. Planning(贪心)
- Codeforces Round #433 (Div. 2)-贪心-C. Planning
- Codeforces 853A Planning 【贪心】
- [codeforces] 854C. Planning(优先队列)
- (codeforces 853A)Planning 贪心
- Codeforces Round#433 C Planning
- Codeforces Round #433 (Div. 2, C. Planning (贪心+简单实现)
- Codeforces Round #433 (2)C:Planning
- Codeforces Round #433 (Div. 2) C. Planning
- Codeforces Round #433 (Div. 2) C. Planning
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- 【贪心+并查集】Codeforces 853A Planning
- Planning CodeForces
- Planning CodeForces
- 关于c/c++中static
- czl蒻蒟的OI之路5
- MediaPlayer
- mysql 索引优化、使用原则及注意事项
- 栈
- Codeforces 854C. Planning 贪心
- IOS中子线程刷新UI
- PHP采集(远程抓取) 自定义函数
- 使用 vue-cli 搭建项目
- java常量池
- Linux-centos下安装nginx和tengine
- 1087. All Roads Lead to Rome (30)
- [转]经典人事面面试题
- oracle 10.2g centos7.2上静默安装步骤