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\n",res );
    for( int i = 1; i < n; i++ ) {
        printf( "%lld ", ans[i] );
    }
    printf( "%lld\n", ans[n] );
    return 0;
}
View Code

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

转载于:https://www.cnblogs.com/FriskyPuppy/p/7633111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值