当前位置:网站首页>Package Delivery(贪心)
Package Delivery(贪心)
2022-07-29 10:54:00 【Selvaggia】
传送门
题意:接下来n天中纷纷有快递到达,已知快递到达时间和最晚取回时间,每次可取k件快递,问最少几次取走所有快递
贪心思想,尽量晚取,在a[i].r上取
将各个快递按到达的时间排序,依次与now相比,在now之前到达的收入队列
注意now在不断更新,首先取先到达的截至日期,再到达的物品的r 来更新now(这个我不太能想到)
now从队列中的a[i].r取(r小的拍前),在别的商品的截止日期时取完的 从队列中删去
若一次性把队列中的全部取完,就继续遍历后面进来的物品
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
//#define int long long int
#define pii pair<int,int>
const int N=1e5+5;
struct node{
int l,r;
bool operator<(const node&b)const{
return l<b.l;//按到达的时间排序,依次与now相比
}
}a[N];
signed main(){
int t,n,k,l,r;
cin>>t;
while(t--){
//最少拿多少次
// cin>>n>>k;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++){
// cin>>a[i].l>>a[i].r;
scanf("%d %d",&a[i].l,&a[i].r);
}
sort(a,a+n);
priority_queue<pii,vector<pii>,greater<pii> > Q;
while(!Q.empty())Q.pop();
Q.push({
a[0].r,a[0].l});
int res=0;
int now=a[0].r;
int i=1;
while(i<n){
//遍历先后送达的快递
while(i<n&&a[i].l<=now) {
//now必须去取一次,看看还能取走哪些
now=min(now,a[i].r);//now也要在到达的快递里面找最先过期的
Q.push({
a[i].r,a[i].l});
i++;//继续看下一件
}
// if(!Q.empty()){//可不能会为空
res++;
int s=Q.size();
if(s<=k){
while(!Q.empty())Q.pop();
if(i<n){
now=a[i].r;
Q.push({
a[i].r,a[i].l});
i++;
}
}
else{
int c=k;
while(c--)Q.pop();//把最先到截止日期的k个取走,否则过期
now=Q.top().first;
}
}
res+=(Q.size()+k-1)/k;//向上取整,可能最后一次可取走的大于k
cout<<res<<endl;
}
return 0;
}
边栏推荐
- ADDS:使用 PowerShell 创建 OU 结构
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- Qt 之自定义界面(实现无边框、可移动)
- Basic construction of QT project
- R 语言 二分法与 牛顿迭代法计算中方程的根
- Software testing dry goods
- Kunlunbase instruction manual (I) quick installation manual
- Getting started with pytoch
- 一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
- 学习R语言这几本电子书就够了!
猜你喜欢

Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29

Learning R language these ebooks are enough!

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

【图像处理】基于中轴变换实现图像骨架提取附matlab代码

Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open

Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29

Leetcode binary tree series -- 144. Preorder traversal of binary trees

浅谈安科瑞灭弧式智慧用电在养老机构的应用

If distributed file storage is realized according to integrated Minio

DNS协议、ICMP协议、NAT技术
随机推荐
Determine whether the values of two objects are equal
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
LeetCode_416_分割等和子集
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
JS two array objects for merging and de duplication
ggdag 绘制DAG和因果图
Roots of equations in R language dichotomy and Newton iteration
大伟 GBase8s游标稳定性读ESQL测试用例
学习R语言这几本电子书就够了!
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
VMware: use commands to update or upgrade VMware esxi hosts
Start from scratch blazor server (3) -- add cookie authorization
聊聊性能测试环境搭建
Kunlunbase instruction manual (III) data import & synchronization
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
『知识集锦』一文搞懂mysql索引!!(建议收藏)
Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase
Detailed arrangement of JVM knowledge points (long text warning)
Niuke net brush questions
Error: Protobuf syntax version should be first thing in file