(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已经晚了...
#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 贪心的更多相关文章
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- Codeforces 853A Planning
题意 给出飞机单位晚点时间代价和原定起飞时间,现在前k分钟不能起飞,求付出的最小代价和起飞顺序 思路 构造两个优先队列q1,q2,q1按时间顺序,q2按代价顺序,初始将所有飞机入q1,将时间在k前的飞 ...
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- codeforces 854C.Planning 【贪心/优先队列】
Planning time limit per test 1 second memory limit per test 512 megabytes input standard input outpu ...
- Codeforces 854C Planning 【贪心】
<题目链接> 题目大意: 表示有n架飞机本需要在[1,n]时间内起飞,一分钟只能飞一架.但是现在[1,k]时间内并不能起飞,只能在[k+1,k+n]内起飞.ci序号为i的飞机起飞延误一分钟 ...
- Codeforces 854C Planning(贪心+堆)
贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> # ...
- codeforces round 433 C. Planning 贪心
题目大意: 输入n,k,代表n列航班,初始始发实践为1,2,3分钟以此类推,然后输入n个整数分别代表延迟1分钟第i个航班损失多少钱,然后调整后的始发时间表是这样的,任何一辆航班的始发时间不能在他的初始 ...
- codeforces 724D(贪心)
题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
随机推荐
- shell 一些题目
在a.log中精确查找含有msyql单词的行a.log文件内容如下: mysqlmysql mysqlmysql aa mysql_mysqla mysql b_mysql aa _mysqla _m ...
- 初识OpenCV-Python - 007: 平滑图像
本节内容主要将如何平滑图像.如通过低通道滤波模糊图像.或者自定义滤波处理图像. import cv2import numpy as npfrom matplotlib import pyplot as ...
- OA系统和ERP系统的区别
一.OA和ERP的区别 1.含义不同: OA指Office Automation,中文简称自动办公系统,帮助企业内部管理沟通的工具,比如新闻公告.内部沟通.考勤.办公.员工请假.审批流程等. ERP指 ...
- pom.xml文件配置maven仓库地址
中央仓库就是Maven的一个默认的远程仓库,Maven的安装文件中自带了中央仓库的配置($M2_HOME/lib/maven-model-builder.jar) 在很多情况下,默认的中央仓库无法满足 ...
- Eclipse 常用快捷键 (动画讲解)【转】
Eclipse 常用快捷键 (动画讲解)[转] Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的 ...
- 亲历者说:Kubernetes API 与 Operator,不为人知的开发者战争
如果我问你,如何把一个 etcd 集群部署在 Google Cloud 或者阿里云上,你一定会不假思索的给出答案:当然是用 etcd Operator! 实际上,几乎在一夜之间,Kubernetes ...
- 容斥原理——hdu3208
和hdu2204有点像 这题要特别注意精度问题,如pow的精度需要自己搞一下,然后最大的longlong可以设为1<<31 /* 只要求[1,n]范围内的sum即可 那么先枚举幂次k[1, ...
- JS 计算时间范围,最近一周、一个月
//最近一周 getDay(-7) 返回的是距离当前日期的一周后的时间//一月 getDay(-30)//一年 getDay(-365) function getDay(day){ var today ...
- CSS中关于多个class样式设置的不同写法
html中: <div class="containerA"> 这是AAAAAAAAAAAAAAAAAAAAAAA样式 <div class="cont ...
- NtQuerySystemInformation 枚举进程
函数原型: NTSTATUS WINAPI NtQuerySystemInformation( _In_ SYSTEM_INFORMATION_CLASS SystemInformat ...