CF854C Planning优先队列|set
C. 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.
题意:有n架飞机,他们原本的起飞时间依次是1~n,但前k分钟不能起飞,已知第i架飞机延误每1min花费为ci,且不能比原定时间早起飞问每架飞机在第几分钟起飞花费最少。
题解:因为不能比原定时间早起飞,我们先将在第k+i秒可以起飞的飞机放到优先队列里面,再将每分钟花费高的或者花费相同时间比较前的弹出来给他先安排时间起飞。(用set也可(set也是用于排序))
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + ;
struct node{
int id,c;
bool operator < (const node &x)const {
if (c == x.c) return id < x.id;
return c < x.c;
}
}a[N],x;
int ans[N];
int main(){
int n,k;
scanf("%d%d",&n,&k);
for (int i = ; i <= n; i++) {
scanf("%d",&a[i].c);
a[i].id = i;
}
ll sum = ;
priority_queue<node> q;
for (int i = ; i <= k; i++) q.push(a[i]);
for (int i = k+; i <= k+n; i++) {
if (i<=n) q.push(a[i]);
x = q.top();
q.pop();
sum += 1ll*(i-x.id)*x.c;
ans[x.id] = i;
}
printf("%lld\n", sum);
for (int i = ; i <= n; i++)
printf("%d%c", ans[i],i==n?'\n':' ');
return ;
}
优先队列
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + ;
struct node{
int id,c;
}a[N],x;
bool cmp(node i,node j) {
if (i.c == j.c) return i.id > j.id;
return i.c>j.c;
}
int ans[N];
set<int> s;
int main(){
int n,k;
scanf("%d%d",&n,&k);
for (int i = ; i <= n; i++) {
scanf("%d",&a[i].c);
a[i].id = i;
s.insert(i+k);
}
sort(a+,a+n+,cmp);
ll sum = ;
for (int i = ; i <= n; i++) {
int x = *s.lower_bound(a[i].id);
sum += 1ll*(x-a[i].id)*a[i].c;
ans[a[i].id] = x;
s.erase(x);
}
printf("%lld\n", sum);
for (int i = ; i <= n; i++)
printf("%d%c", ans[i],i==n?'\n':' ');
return ;
}
Set
CF854C Planning优先队列|set的更多相关文章
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- codeforces 854C.Planning 【贪心/优先队列】
Planning time limit per test 1 second memory limit per test 512 megabytes input standard input outpu ...
- #433 Div2 Problem C Planning (贪心 && 优先队列)
链接 : http://codeforces.com/contest/854/problem/C 题意 : 有 n 架飞机需要分别在 1~n 秒后起飞,允许起飞的时间是从 k 秒后开始,给出每一架飞机 ...
- Planning CodeForces - 854C
Planning CodeForces - 854C 题意:有n架航班,第i架原先的时候是在第i分钟起飞的.现在前k分钟无法有飞机起飞,因此需要调整安排表,延后飞机起飞.仍然要求每一分钟只有一架飞机起 ...
- B - Planning 早训 贪心
B - Planning 这个题目我知道要贪心,也知道怎么贪,但是写不出来,感觉自己好菜. 这个题目要用优先队列维护. 题目大意是飞机延误,不同的飞机每次延误一分钟,它的代价不同,然后问,怎么安排才能 ...
- 敏捷转型历程 - Sprint3 Planning
我: Tech Leader 团队:团队成员分布在两个城市,我所在的城市包括我有4个成员,另外一个城市包括SM有7个成员.另外由于我们的BA离职了,我暂代IT 的PO 职位.PM和我在一个城市,但他不 ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
随机推荐
- vbox ubuntu虚拟机中加载笔记本内置摄像头
使用C:\Program Files\Oracle\VirtualBox\VBoxManage.exe工具加载摄像头 1,显示可用摄像头 C:\Program Files\Oracle\Virtual ...
- PHP redis安装扩展
命令: 查看php版本:PHP -v 查看php安装的扩展:PHP -m php扩展开发包(包括phpize,php -config):yum install php-devel which phpi ...
- Python--day30--软件开发架构
软件开发架构: C/S架构: B/S架构: B/S架构和C/S架构的关系:
- [转载] linux、Solaris下xdmcp远程桌面服务
原文链接 http://youlvconglin.blog.163.com/blog/static/52320420106243857254/ 使用图形界面远程登录linux和Solaris,首先要在 ...
- [转载] 使用StAX解析xml
StAX 概述 从一开始,Java API for XML Processing (JAXP) 就提供了两种方法来处理 XML:文档对象模型(DOM)方法是用标准的对象模型表示 XML 文档:Simp ...
- 2018-11-2-win10-uwp-通过-win2d-画出笔迹
title author date CreateTime categories win10 uwp 通过 win2d 画出笔迹 lindexi 2018-11-2 20:11:0 +0800 2018 ...
- 基于ElementUI封装Excel数据导入组件
由于前端项目使用的是Vue-cli3.0 + TypeScript的架构,所以该组件也是基于ts语法封装的,组件的完整代码如下: <template> <div id="m ...
- 51nod 1281山峰和旗子
1281 山峰和旗子 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 用一个长度为N的整数数组A,描述山峰和山谷的高度.山峰需要满足 ...
- Python涉及的各个领域以及技术应用
WEB开发 完全主义者高效率框架Django 异步高并发Tornado框架 短小精悍Flask,Bottle框架 网络编程 高并发Twisted网络框架 Python3引入的asyncio异步编程 爬 ...
- dotnet core 获取 MacAddress 地址方法
本文告诉大家如何在 dotnet core 获取 Mac 地址 因为在 dotnetcore 是没有直接和硬件相关的,所以无法通过 WMI 的方法获取当前设备的 Mac 地址 但是在 dotnet c ...