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. C++开发系列-C语言的malloc与C++的new分配空间

    概述 在软件开发过程中,常常需要动态地分配和撤销存储空间,例如对动态链表中结点的插入与删除.在C语言中是利用库函数malloc和free来分配和撤销内存空间的.C++提供了较简便而功能较强的运算符ne ...

  2. 2019-11-7-C#-dotnet-线程不安全的弱引用缓存

    title author date CreateTime categories C# dotnet 线程不安全的弱引用缓存 lindexi 2019-11-7 9:45:5 +0800 2019-11 ...

  3. java系统监控分析Jprofile下载及安装配置【转】

    JProfiler是一个全功能的Java剖析工具(profiler),专用於分析J2SE和J2EE应用程式.它把CPU.线程和记忆体的剖析组合在一个强大的应用中.JProfiler可提供许多IDE整合 ...

  4. IDEA将代码推送至远程GitHub仓库

    1 在项目根路径下添加.gitignore文件 2 创建本地git仓库 3 git add操作 快捷键 ctrl+alt+a 4 git commit操作 快捷键ctrl+k 5 git push操作 ...

  5. F. Cowmpany Cowmpensation dp+拉格朗日插值

    题意:一个数,每个节点取值是1-d,父亲比儿子节点值要大,求方案数 题解:\(dp[u][x]=\prod_{v}\sum_{i=1}^xdp[v][i]\),v是u的子节点,先预处理出前3000项, ...

  6. 第十三章 Odoo 12开发之创建网站前端功能

    Odoo 起初是一个后台系统,但很快就有了前端界面的需求.早期基于后台界面的门户界面不够灵活并且对移动端不友好.为解决这一问题,Odoo 引入了新的网站功能,为系统添加了 CMS(Content Ma ...

  7. threading.local在flask中的用法

    一.介绍 threading.local的作用: 多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据的存储,而每块空间内的数据也不会错乱. 二.不使用threading. ...

  8. 【转】浅析BFC及其作用

    1. 什么是BFC BFC(block formatting context):简单来说,BFC 就是一种属性,这种属性会影响着元素的定位以及与其兄弟元素之间的相互作用. 中文译为块级格式化上下文.是 ...

  9. MyEclipse使用总结——在MyEclipse中新建Maven框架的web项目[转]

    前面的文章我们已经在本机安装好了maven,同时在myeclipse中配置好了maven的插件. 链接如下: Maven安装----在Windows上安装Maven myeclipse安装maven插 ...

  10. 杂项-公司:Facebook

    ylbtech-杂项-公司:Facebook Facebook(脸书)是美国的一个社交网络服务网站 ,创立于2004年2月4日,总部位于美国加利福尼亚州帕拉阿图,2012年3月6日发布Windows版 ...