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.
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.
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.
5 2
4 2 1 10 2
20
3 6 7 4 5
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.
新号第一次打cf,由于STL不熟练,手写优先队列写挂了,止步c题,还需努力.
题意:本来安排了n架飞机,每架飞机有mi的重要度,第i架飞机的起飞时间原定为i,而现在在k时之前不能有任何飞机起飞,每个时间点只有1架飞机能起飞,损失被定义为(飞机现起飞时间-飞机原起飞时间)*该飞机的重要度.现在要求你排一张时间表,要求每架飞机都只能在原起飞时间及以后起飞,且使损失最小.
题解:显然是一道贪心,值越大的越先起飞,造成损失越小,因为假设a>b , a*n+b*(n+1)=(a+b)*n+b (1) , a*(n+1)+b*n=(a+b)*n+a (2).显然(1)>(2).所以枚举时间,将1-k之间的飞机先压入优先队列,接着k-n+k一边加进原定当时起飞的飞机,一边找到优先队列中的最大值即队顶元素,将它放在该位置,更新它的新的起飞时间.这样可以保证i时进来的飞机一定在i之后起飞.
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 300010
using namespace std; struct NODE
{
int i,w;
bool operator <(const NODE&a)const
{
return w<a.w;
}
}p[N]; int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].w);
p[i].i=i;
}
priority_queue<NODE>q;
for(int i=;i<=k;i++)
{
q.push(p[i]);
}
long long sum=;
for(int i=k+;i<=n+k;i++)
{
if(i<=n)
{
q.push(p[i]);
}
NODE now=q.top();
q.pop();
sum+=(long long) (i-now.i)*now.w;
p[now.i].i=i;
}
printf("%lld\n",sum);
for(int i=;i<=n;i++)
{
printf("%d ",p[i].i);
}
return ;
}
每天刷题,身体棒棒!
CodeForces - 853A Planning (优先队列,贪心)的更多相关文章
- 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 853A Planning
		
题意 给出飞机单位晚点时间代价和原定起飞时间,现在前k分钟不能起飞,求付出的最小代价和起飞顺序 思路 构造两个优先队列q1,q2,q1按时间顺序,q2按代价顺序,初始将所有飞机入q1,将时间在k前的飞 ...
 - codeforces 704B - Ant Man 贪心
		
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
 - CodeForces - 50A Domino piling (贪心+递归)
		
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
 - 最高的奖励 - 优先队列&贪心 / 并查集
		
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
 - POJ2431 优先队列+贪心 - biaobiao88
		
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
 - hdu3438 Buy and Resell(优先队列+贪心)
		
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
 
随机推荐
- Bootstrap框架的了解和使用之栅格系统
			
前 言 Bootstrap Bootstrap 包含了一个响应式的.移动设备优先的.不固定的网格系统,可以随着设备或视口大小的增加而适当地扩展到 12 列.它包含了用于简单的布局选项的预定 ...
 - 初识Hibernate之环境搭建
			
相信所有做后端的程序员同行们,没有不知道Hibernate大名的.这是一个经典的轻量级Java EE持久层的解决方案,它使得我们程序员能以面向对象的思维操作传统的关系型数据库,这也是其存在的 ...
 - JAVA多线程---wait() & join()
			
题外话: interrupt()方法 并不能中断一个正常运行的线程!!! class myThread extends Thread{ @Override public void run(){ fo ...
 - Tree(uva 536)
			
先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...
 - 第5章   不要让线程成为脱缰的野马(Keeping your Threads on Leash)  ---线程优先权(Thread priority)
			
有没有过这样的经验?你坐在你的车子里,目的地还在好几公里之遥,而时间已经很晚了.你拼命想告诉那些挡住你去路的人们,今天这个约会对你是多么多么重要,能不能请他们统统--呃--滚到马路外?很不幸,道路系统 ...
 - bzoj3624(铺黑白路)(并查集维护)
			
题意网上自己随便找,绝对是找的到的. 题解:(白边表示鹅卵石路,黑边表示水泥路)这道题的解法,先考虑将黑边所有都先连起来,组成一个又一个的联通块,然后用白边去连, 如果可以联通的话,就用白边去代替黑边 ...
 - 笨鸟先飞之ASP.NET MVC系列之过滤器(02授权过滤器)
			
授权过滤器 概念介绍 在之前的文章中我们已经带大家简单的了解了下过滤器,本次我们开始介绍授权过滤器. 我们之前提到过授权过滤器在认证过滤器之后,其他过滤器和方法被调用之前运行,而授权过滤器和它名字的含 ...
 - H5音频处理的一些小知识
			
前 言 LiuDaP 十一过后,小编要做一个关于音乐播放器的项目,要用到大量H5音频处理的内容,于是在十月一日国庆黄金周闲暇之际,自己学习了一下H5音频的相关内容.虽然自学的没有那么深入,但是对 ...
 - mysqldumpslow -- 分析慢查询日志
			
格式:mysqldumpslow [选项] 慢查询日志路径 选项: -s 排序方式,可选值有c(记录次数).t(查询时间).l(锁定时间).r(返回记录).a(平均) -t 显示的记录数 - ...
 - 从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置
			
第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分 ...