Codeforces 854 C Planning(优先队列+贪心)

来源:互联网 发布:mac 能玩魔兽争霸 编辑:程序博客网 时间:2024/05/20 06:22

题目地址
题意:有个机场他因为某些原因只能在时间k以后进行起飞,起飞的顺序是可以调的,只有一个限制就是不能延误之后的时间比正常起飞的时间好要早,每个航班没延误1分钟就要多花费ki块钱,问最小的花费。
思路:贪心的思想,每次选要花费最小的,然后对于限制条件的维护就是让大于m的在过来这个时间后再往里面添加。

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#include <iomanip>#define N 300010#define M 555005  #define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1000000007;struct node {    LL num;    int id;    friend bool operator <(node a, node b){        return a.num < b.num;    }}fly[N], now;int times[N];int main() {    cin.sync_with_stdio(false);    int n, m;    priority_queue<node> q;    while (cin >> n >> m) {        while (!q.empty()) { q.pop(); }        for (int i = 1; i <= n; i++) {            cin >> fly[i].num;            fly[i].id = i;        }        LL sum = 0;        LL ans = m + 1;        for (int i = 1; i <= m; i++) {            q.push(fly[i]);        }        while (!q.empty()) {            if (ans <= n) q.push(fly[ans]);            now = q.top();            q.pop();            LL id = now.id;            times[id] = ans;            sum += (ans - id)*fly[id].num;            ans++;        }        cout << sum << endl;        for (int i = 1; i <= n; i++) {            cout << times[i] << " ";        }        cout << endl;    }    return 0;}
阅读全文
0 0
原创粉丝点击