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已经晚了...

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cstdio>
#include <queue>
#pragma warning ( disable : 4996 )
#define PERMAX 2 using namespace std; int Max( int x, int y ) { return x>y?x:y; }
int Min( int x, int y ) { return x>y?y:x; } const int inf = 0x3f3f3f3f;
const int vspot = 3e5 + ;
const int espot = 1e5 + ; struct node {
int id, cost; bool operator < ( const node &x ) const
{ return cost < x.cost; } //cost大于x.cost时返回false,此时cost优先级更高
}p[vspot];
int N, K, tim[vspot]; int main()
{
while( ~scanf("%d %d", &N, &K) )
{
priority_queue<node> Q;
long long ans = ;
for ( int i = ; i <= N; i++ )
{ scanf("%d", &p[i].cost); p[i].id = i; } for ( int i = ; i <= K; i++ )
Q.push(p[i]); int id, cost;
for ( int i = K+; i <= N+K; i++ )
{
if (i<=N)
Q.push(p[i]); id = (Q.top()).id; cost = (Q.top()).cost; Q.pop();
tim[id] = i;
ans += (long long)(i-id)*cost; //注意外面不能再加括号了
}
printf("%lld\n", ans);
for( int i = ; i < N; i++ )
printf( "%d ", tim[i] );
printf( "%d\n", tim[N] );
}
return ;
}

l

(codeforces 853A)Planning 贪心的更多相关文章

  1. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  2. Codeforces 853A Planning

    题意 给出飞机单位晚点时间代价和原定起飞时间,现在前k分钟不能起飞,求付出的最小代价和起飞顺序 思路 构造两个优先队列q1,q2,q1按时间顺序,q2按代价顺序,初始将所有飞机入q1,将时间在k前的飞 ...

  3. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

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

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

  5. Codeforces 854C Planning 【贪心】

    <题目链接> 题目大意: 表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟 ...

  6. Codeforces 854C Planning(贪心+堆)

    贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> # ...

  7. codeforces round 433 C. Planning 贪心

    题目大意: 输入n,k,代表n列航班,初始始发实践为1,2,3分钟以此类推,然后输入n个整数分别代表延迟1分钟第i个航班损失多少钱,然后调整后的始发时间表是这样的,任何一辆航班的始发时间不能在他的初始 ...

  8. codeforces 724D(贪心)

    题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...

  9. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

随机推荐

  1. Nand Flash 控制器工作原理

    对 Nand Flash 存储芯片进行操作, 必须通过 Nand Flash 控制器的专用寄存器才能完成.所以,不能对 Nand Flash 进行总线操作.而 Nand Flash 的写操作也必须块方 ...

  2. string json list

    String str="[{\"cIndex\":14,\"column\":\"nextAdvice\",\"id\& ...

  3. 字符串+dp——cf1163D好题

    很好的题(又复习了一波kmp) /* dp[i,j,k]:到s1的第i位,匹配s2到j,s3到k的最优解 */ #include<bits/stdc++.h> using namespac ...

  4. AnsiString, String, char,char

    AnsiString 是一个类,String 是一个结构,char* 是一个指针 .String是Pascal的类型原型,因为C++中没有字符串数据类型的,因此使用char*来存储,char*必须是以 ...

  5. 洛谷P1129 【ZJOI2007】矩阵游戏

    原题传送门 题目描述 小QQ是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N \times NN×N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每 ...

  6. We'll be fine.

    可怜的人心中都有一种信念,那就是 明天会更好. Everything will be fine. 我拥见风来,嗅见花开,是避不掉的厉寒,是止不住的徘徊.

  7. laravel中如何使用Redis(Redis是什么)

    laravel中如何使用Redis(Redis是什么) 一.总结 一句话总结: 基于内存亦可持久化键值数据库 Redis是完全开源免费的,遵守BSD协议,是一个高性能的键值数据库.是当前最热门的的的N ...

  8. 2016年深圳市服务业占GDP比重首次突破六成

    2016年深圳市服务业占GDP比重首次突破六成 中商产业研究院 中商情报网 2017-01-12 11:08 分享:     中商情报网讯 1月10日,深圳市财政委员会召开新闻发布会,就深圳市2016 ...

  9. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  10. 力扣算法题—146LRU缓存机制

    [题目] 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...