Planning CodeForces - 853A 贪心

题目链接:
Planning

题目大意:
机场中飞机起飞序列为 a i ( i >= 0 ) 分别表示为: 在 i 时间起飞的飞机被延误后的每分钟代价为 a i
现在前 k 个飞机都被延误了,
求怎么安排飞机起飞才能使得代价最小
要求: 飞机不能比原先的时间提早起飞
输出每个飞机的起飞时间

样例:
Input
5 2
4 2 1 10 2
Output
20
3 6 7 4 5

the best schedule is shown in the sample answer, its cost is (3 - 1)·4 + (6 - 2)·2 + (7 - 3)·1 + (4 - 4)·10 + (5 - 5)·2 = 20 burles.

分析:
贪心地去考虑 ,在可以起飞的飞机序列里选择代价最大的先起飞即可,
注意细节处理
按照时间轴去处理就可以避免判断飞机是否可飞.
从k~n+k循环处理
用 pii 来存储 id
贪心的很多题都用 pii 实现

具体实现:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define inf (1e9+10)
#define mem(s,t) memset(s,t,sizeof s)

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")


typedef long long ll;
typedef pair<int,int> pii;
const int MAXN =3e5+10;

priority_queue<pii> pq;
pii c[MAXN];
int ret[MAXN];

int main() {
    int n,k;
    scanf("%d%d",&n,&k);

    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        c[i]=make_pair(x,i);
    }

    ll ans=0;
    for(int i=0;i<k;i++) pq.push(c[i]);
    for(int i=k;i<k+n;i++){
        if(i<n) pq.push(c[i]);//不能一次push 要随时间一个个push
        pii t=pq.top();
        pq.pop();
        ans+=1LL*(i-t.second)*t.first;//计算总代价
        ret[t.second]=i;//记录答案
    }
    printf("%lld\n",ans);
    for(int i=0;i<n;i++) printf("%d ",ret[i]+1);
    puts("");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/joovo/article/details/79211289