当前位置:网站首页>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;
}
边栏推荐
- AI模型风险评估 第2部分:核心内容
- 通过tidymodels使用XGBOOST
- 阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!
- 大伟 GBase8s游标稳定性读ESQL测试用例
- JS two array objects for merging and de duplication
- 报表控件FastReport与StimulSoft功能对比
- Use tidymodels to solve the binary logistic model
- 开源峰会抢先看 | 7月29日分论坛&活动议程速览
- 多线程顺序运行的 4 种方法,面试随便问!
- Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
猜你喜欢

Leetcode bit operation

报表控件FastReport与StimulSoft功能对比

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

Kunlunbase instruction manual (I) quick installation manual

Pytorch 入门

QWidget、QDialog、QMainWindow 的异同点

AI模型风险评估 第2部分:核心内容

DoD 和 DoR,消减「认知偏差」的两大神器

Basic construction of QT project

【图像处理】基于中轴变换实现图像骨架提取附matlab代码
随机推荐
leetcode-位运算
sql join中on条件后接and和where
大伟 GBase8s游标稳定性读ESQL测试用例
JVM知识点详细整理(长文警告)
Steps of project explanation in interview
PaddleLite 编译以及代码跑通复盘
If distributed file storage is realized according to integrated Minio
使用R包skimr汇总统计量的优美展示
建议收藏丨sql行转列的一千种写法!!
带你浅聊一下PHP搭建的电商商城系统
Using R-Pack premsim to predict microsatellite instability based on gene expression
Kunlunbase instruction manual (I) quick installation manual
R语言 使用数据集 veteran 进行生存分析
Niuke net brush questions
TCP和UDP
QT工程基本构建
QT基本工程的解析
Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase
浅谈安科瑞灭弧式智慧用电在养老机构的应用
Luogu p4185 [usaco18jan]mootube g problem solution