Codeforces 854 C Planning 贪心 最大堆

  题目链接: https://vjudge.net/problem/CodeForces-854C

  题目描述: 有n架飞机,第i架飞机原本计划在第i分钟起飞,可是由于某种原因整个机场前k分钟是不能起飞的,每分钟只能起飞一架飞机,然后告诉你每架飞机每延误一分钟会造成的损失,问你如何安排飞机的起飞时间才能将损失降到最少(飞机不能提前起飞)。

  解题思路: 我们首先的想法就是能不能贪心的选取花费大的排在前面, 因为最坏情况也就是cost 差了1, 而且时间差了1, 这样两种情况是相等的, 剩下的所有的贪心的选取都比不贪心要优, 剩下就是必须在Ks之后起飞这个条件了, 我们只要在k次输入后再从有限队列中取值就可以了

  代码: 

#include <iostream>
#include <cstdio>
#include <map>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;

struct node {
    ll t;
    ll w;
    bool operator < ( const node & tmp ) const {
       return w == tmp.w ? t > tmp.t : w < tmp.w;
    }
    node(ll tt, ll ww): t(tt), w(ww) {}
};

priority_queue<node> q;
const int maxn = 3e5+10;
ll ans[maxn];

int main() {
    ll n, k;
    scanf( "%lld%lld", &n, &k );
    ll res = 0;
    for( int i = 1; i <= n+k; i++ ) {
        if( i <= n ) {
            ll w;
            scanf("%lld", &w);
            q.push(node(i,w));
        }
        if( i > k ) {
            node nn = q.top();
            q.pop();
            res += (i-nn.t)*nn.w;
            ans[nn.t] = i;
        }
    }
    printf( "%lld
",res );
    for( int i = 1; i < n; i++ ) {
        printf( "%lld ", ans[i] );
    }
    printf( "%lld
", ans[n] );
    return 0;
}
View Code

  思考: 自己没有想到STL真的是蠢啊

原文地址:https://www.cnblogs.com/FriskyPuppy/p/7633111.html