当前位置: 首页 > news >正文

奶牛用餐 优先队列 java

👨‍🏫 奶牛用餐

约翰的农场有 n n n 头奶牛,编号 1 s i m n 1 \\sim n 1simn

每天奶牛们都要去食堂用餐。

食堂一共有 k k k 个座位,也就是说同一时间最多可以容纳 k k k 头奶牛同时用餐。

已知,第 i i i 头奶牛到达食堂的具体时刻为 s _ i s\_i s_i,用餐所需花费的时间为 t _ i t\_i t_i

保证 s _ 1 < s _ 2 < … < s _ n s\_1 < s\_2 < … < s\_n s_1<s_2<<s_n

为了让奶牛们有序用餐,约翰制定了如下规则:

  • 每头奶牛都必须由约翰安排座位用餐。
  • 每头奶牛从到达食堂的那一刻起,即刻进入待安排状态。
  • 任意时刻,只要存在空座位以及待安排奶牛,约翰就会即刻安排奶牛就座用餐。
  • 如果某一时刻,空座位的数量少于待安排奶牛的数量,则优先安排编号更小的奶牛就座用餐。
  • 每头奶牛用餐完毕的那一时刻都会被约翰立即轰走,即刻空出座位。

除了用餐花费时间以外,其它花费时间忽略不计。

请你计算并输出,每头奶牛用餐完毕的具体时刻。

输入格式

第一行包含两个整数 n , k n,k n,k

接下来 n n n 行,其中第 i i i 行包含两个整数 s _ i , t _ i s\_i,t\_i s_i,t_i

注意,输入保证 s _ 1 < s _ 2 < … < s _ n s\_1 < s\_2 < … < s\_n s_1<s_2<<s_n

输出格式

n n n 行,每行输出一个整数,其中第 i i i 行的整数表示第 i i i 头奶牛用餐完毕的具体时刻。

数据范围

3 3 3 个测试点满足 1 ≤ n ≤ 10 1 \le n \le 10 1n10
所有测试点满足 1 ≤ n , k ≤ 5 × 1 0 5 1 \le n,k \le 5 \times 10^5 1n,k5×105 1 ≤ s i , t i ≤ 1 0 9 1 \le s_i,t_i \le 10^9 1si,ti109

输入样例1:

3 2
1 5
2 5
3 5

输出样例1:

6
7
11

输入样例2:

6 1
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000
6 3

输出样例2:

1000000001
2000000001
3000000001
4000000001
5000000001
5000000004

🍺 AC code


import java.io.*;
import java.util.*;public class Main
{static int N = 500050;static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));public static void main(String[] args) throws IOException{
//		Scanner sc = new Scanner(System.in);
//		int n = sc.nextInt();
//		int m = sc.nextInt();String[] ss = in.readLine().split(" ");int n = Integer.parseInt(ss[0]);int m = Integer.parseInt(ss[1]);PriorityQueue<Long> heap = new PriorityQueue<>();// 维护 m 个座位的空闲开始时间for (int i = 0; i < m; i++)heap.add(0L);// 座位的空闲时间初始化为 0while (n-- > 0){
//			long start = sc.nextLong();
//			long time = sc.nextLong();ss = in.readLine().split(" ");long start = Long.parseLong(ss[0]);long time = Long.parseLong(ss[1]);long t = heap.poll();// 每次获取最快有空位的时间long end = Math.max(start, t) + time;// 和自身的到达时间取 max 值heap.add(end);// 把此座位的下一次空位时刻加入 堆System.out.println(end);}}
}
http://www.lryc.cn/news/120910.html

相关文章:

  • 包管理机制pip3
  • liunx在线安装tomcat
  • 导入示例工程出现error: failed to start ability. Error while Launching activity错误的解决办法
  • 【深入了解PyTorch】PyTorch分布式训练:多GPU、数据并行与模型并行
  • linux 下 网卡命名改名
  • 6.2.0在线编辑:GrapeCity Documents for Word (GcWord) Crack
  • 为什么需要智能指针?
  • 《华为认证》L2TP VPN配置
  • 【JVM】JVM垃圾收集器
  • StarGANv2: Diverse Image Synthesis for Multiple Domains论文解读及实现(一)
  • Go Gin 中使用 JWT
  • AWS中Lambda集成SNS
  • Mac下⬇️Git如何下载/上传远程仓库
  • linux 命令--常用关机命令
  • ttf-dejavu fontconfig字体
  • Open3D点云数据处理(十九):最小二乘直线拟合(矩阵方程法)
  • 数据库事务ACID介绍
  • SM8650 qcxserver.c STRM_Initialize
  • 适配器模式-java实现
  • 【elasticSearch系】3.完整搭建详尽版elk
  • 代码随想录day04
  • [Realtek] WPA_SUPPLICANT + WPA_CLI使用指南
  • # ⛳ Docker 安装、配置和详细使用教程-Win10专业版
  • Linux 教程
  • 图论——最短路算法
  • 在项目中增加网络加载需要考虑什么?
  • 阿里云服务器部署RabbitMQ流程
  • 青大数据结构【2014】
  • Ansible Playbook快速部署一主多从MySQL集群
  • 27.Netty源码之FastThreadLocal