Codeforces 853A Planning 【贪心】

22 篇文章 0 订阅
13 篇文章 0 订阅

题目大意
由于不可抗力什么的所有航班都要推迟k分钟,啊但是每架飞机都已经计划好原来的起飞计划了Orz(输入顺序就是原起飞计划),于是Helen就需要搞一个新的起飞计划。
飞机C每推迟一分钟(相对于原计划)就会花费C_i块钱,并且新的计划中,不允许某架飞机的起飞时间比原计划还要早,而且而且不允许同一分钟内起飞两架飞机
现在求一个最优方案使得花费最少,并且打印出这个方案来。

一个贪心的想法是优先考虑那些花费最高的飞机,使得这些飞机的延误时间最短,从而尽量降低花费,从k+1至k+n开始模拟,每个时刻i能够起飞的只有前i架飞机,用堆处理最大花费的那架。

还有就是要开longlong

 ans += (long long) (i-x.ti) * x.w;

这一段有必要说一下,(i-x.ti)不会爆int
((i-x.ti)*x.w)会爆int
所以要用(longlong)强制转换(i-x.ti)而不是整个表达式,longlong转换的优先级高于加减乘除
然后longlong型的i-x.ti与x.w相乘,答案也是longlong型

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
const int maxn = 300010;
inline void read(int &x) {
    x = 0;
    char ch = getchar();
    while(ch<'0'||ch>'9') ch = getchar();
    while(ch>='0'&&ch<='9') x = x*10+ch-'0', ch = getchar();
    return;
}
int n,c[maxn],k,work[maxn];
long long ans;
bool time[maxn];
struct st{
    int ti, w;
    bool operator < (const st &a) const{
        if(a.w==w) {
            return a.ti > ti;
        }
        return a.w > w;
    }
};
using namespace std;
priority_queue <st> q;

int main() {
    read(n); read(k);
    for(int i=1; i<=n; i++)
        read(c[i]);
    for(int i=1; i<=k; i++)
        q.push((st){i,c[i]});
    for(int i=k+1; i<=k+n; i++) {
        if(i<=n)
            q.push((st){i,c[i]});
        st x = q.top();
        q.pop();
        ans += (long long) (i-x.ti) * x.w; //强制转换i-x.ti为longlong之后再与x.w相乘 
        work[x.ti] = i;  
    }
    printf("%lld\n",ans);
    for(int i=1; i<=n; i++)
        printf("%d ",work[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值