(codeforces 853A)Planning 贪心

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.


sample input

5 2
4 2 1 10 2

sample output

20
3 6 7 4 5

事实证明想对思路也要会写才行啊...想出来了大概思路是cost花费大的排前面,但没想到用优先队列,导致我在怎么处理飞机不能提前起飞这点上整了半天.结果发现一发优先队列很优雅的就写出来了...另外也算是学到了贪心的证明吧

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

最后要注意一点容易踩坑的,最后类型转换里(long long)(i-id)*cost; 不能写成(long long)((i-id)*cost); 因为把括号写在外面,实际上里面的cost很大,相乘后会超出int范围,一些有效数字已经被舍掉了,这时再转long long已经晚了...

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <queue>
 7 #pragma warning ( disable : 4996 )
 8 #define PERMAX 2
 9 
10 using namespace std;
11 
12 int Max( int x, int y ) { return x>y?x:y; }
13 int Min( int x, int y ) { return x>y?y:x; }
14 
15 const int inf = 0x3f3f3f3f;
16 const int vspot = 3e5 + 5;
17 const int espot = 1e5 + 5;
18 
19 struct node {
20     int id, cost;
21 
22     bool operator < ( const node &x ) const
23         { return cost < x.cost; }    //cost大于x.cost时返回false,此时cost优先级更高
24 }p[vspot];
25 int N, K, tim[vspot];
26 
27 int main()
28 {
29     while( ~scanf("%d %d", &N, &K) )
30     {
31         priority_queue<node> Q;
32         long long ans = 0;
33         for ( int i = 1; i <= N; i++ )
34             { scanf("%d", &p[i].cost); p[i].id = i; }
35 
36         for ( int i = 1; i <= K; i++ )
37             Q.push(p[i]);
38 
39         int id, cost;
40         for ( int i = K+1; i <= N+K; i++ )
41         {
42             if (i<=N)
43                 Q.push(p[i]);
44             
45             id = (Q.top()).id; cost = (Q.top()).cost; Q.pop();
46             tim[id] = i;
47             ans += (long long)(i-id)*cost; //注意外面不能再加括号了
48         }
49         printf("%lld
", ans);
50         for( int i = 1; i < N; i++ )
51             printf( "%d ", tim[i] );
52         printf( "%d
", tim[N] );
53     }
54     return 0;
55 }

l

原文地址:https://www.cnblogs.com/chaoswr/p/8496195.html