codeforces 854C.Planning 【贪心/优先队列】

Planning
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today, the i-th of them is planned to depart at the i-th minute of the day.

Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first k minutes of the day, so now the new departure schedule must be created.

All n scheduled flights must now depart at different minutes between (k + 1)-th and (k + n)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do so — their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule.

Helen knows that each minute of delay of the i-th flight costs airport ci burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 300 000), here n is the number of flights, and k is the number of minutes in the beginning of the day that the flights did not depart.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 107), here ci is the cost of delaying the i-th flight for one minute.

Output

The first line must contain the minimum possible total cost of delaying the flights.

The second line must contain n different integers t1, t2, ..., tn (k + 1 ≤ ti ≤ k + n), here ti is the minute when the i-th flight must depart. If there are several optimal schedules, print any of them.

Example
input
5 2
4 2 1 10 2
output
20
3 6 7 4 5
Note

Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be(3 - 1)·4 + (4 - 2)·2 + (5 - 3)·1 + (6 - 4)·10 + (7 - 5)·2 = 38 burles.

However, the better 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 = 20burles.

【题意】:
给出n(3e5),k(<=n),以及n个数ci(1e7). 
表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟的cost.一个飞机不能比原定时间早起飞,请安排一个起飞顺序,求最小的cost和。

【分析】:

贪心证明

设序号为i的飞机起飞时间为di,则cost=∑(di-i)*ci=∑di*ci-∑i*ci
显然后一项为常数,而{di-k}为[1,n]的一个排列, 
所以只要使ci越大的i尽可能早起飞即可使得cost最小.

求解

对于每个[k+1,k+n]的时刻t,都会有一架飞机起飞, 
而可起飞的飞机只有原定起飞时刻在[1,t]内已经准备好的飞机.从这些飞机中选取ci最大的即可. 
维护一个优先队列.一次循环就可以得出结果.

【代码】:

#include<bits/stdc++.h>   
using namespace std;  
typedef long long ll;  
struct node{  
    int id,cost;  
    node(int id,int cost):id(id),cost(cost){};  
    bool operator<(const node& n)const{return cost<n.cost;}  
};  
int a[300001];  
int main(){  
    int n,k;  
    priority_queue<node>q;  
    while(~scanf("%d%d",&n,&k)){  
        while(!q.empty()) q.pop();  
        int t;  
        ll sum=0;  
        for(int i=1;i<=n+k;i++){  
            if(i<=n){  
                scanf("%d",&t);  
                q.push(node(i,t));  
            }  
            if(i>k){  
                node tt=q.top();  
                q.pop();  
                sum+=(i-tt.id)*tt.cost;  
                a[tt.id]=i;  
            }  
        }  
        printf("%I64d
",sum);  
        for(int i=1;i<n;i++) printf("%d ",a[i]);  
        printf("%d
",a[n]);  
    }  
    return 0;  
}   
#include<bits/stdc++.h>   
using namespace std;  
typedef long long ll;  
struct node{  
    int id,cost;  
    node(int id,int cost):id(id),cost(cost){};  
    bool operator<(const node& n)const{return cost<n.cost;}  
};  
int a[300001];  
int main(){  
    int n,k;  
    priority_queue<node>q;  
    while(~scanf("%d%d",&n,&k)){  
        while(!q.empty()) q.pop();  
        int t;  
        ll sum=0;  
        for(int i=1;i<=n+k;i++){  
            if(i<=n){  
                scanf("%d",&t);  
                q.push(node(i,t));  
            }  
            if(i>k){  
                node tt=q.top();  
                q.pop();  
                sum+=(i-tt.id)*tt.cost;  
                a[tt.id]=i;  
            }  
        }  
        printf("%I64d
",sum);  
        for(int i=1;i<n;i++) printf("%d ",a[i]);  
        printf("%d
",a[n]);  
    }  
    return 0;  
}   
View Code
原文地址:https://www.cnblogs.com/Roni-i/p/7698721.html