当前位置:网站首页>Package delivery (greedy)
Package delivery (greedy)
2022-07-29 10:56:00 【Selvaggia】
Portal
The question : Next n Express delivery arrived one after another in the middle of the day , The delivery arrival time and the latest retrieval time are known , Every time k Express delivery , Ask at least how many times to take all the express
Greedy thoughts , Try to pick it up late , stay a[i].r Pick up
Sort each express according to the time of arrival , And, in turn, now comparison , stay now Previously arrived revenue queue
Be careful now Constantly updating , First, take the first arrival deadline , Of items arriving again r To update now( I can't think of this )
now From the queue a[i].r take (r Small before shooting ), Finished at the deadline of other goods Delete from the queue
If all the items in the queue are taken at one time , Just continue to traverse the items coming in later
#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;// Sort by arrival time , And, in turn, now comparison
}
}a[N];
signed main(){
int t,n,k,l,r;
cin>>t;
while(t--){
// How many times at least
// 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){
// Traverse the express delivered successively
while(i<n&&a[i].l<=now) {
//now You must go and get it once , See what else can be taken
now=min(now,a[i].r);//now You should also find the first expired one in the express delivery that arrives
Q.push({
a[i].r,a[i].l});
i++;// Continue to look at the next
}
// if(!Q.empty()){// It can't be 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();// Put the first to the deadline k Take it away , Otherwise, it will expire
now=Q.top().first;
}
}
res+=(Q.size()+k-1)/k;// Rounding up , Maybe the last time you can take away more than k
cout<<res<<endl;
}
return 0;
}
边栏推荐
猜你喜欢
ES6 arrow function this points to
WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
如何在匹配行之前使用 grep 显示文件名和行号
How can agile development reduce cognitive bias in collaboration| Agile way
北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
如何使用“COPY –link”加速 Docker 构建和优化缓存
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
PaddleLite 编译以及代码跑通复盘
随机推荐
PaddleLite 编译以及代码跑通复盘
PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
LeetCode_416_分割等和子集
How to realize the function of adding watermark
什么是 Kubernetes 自定义资源定义 (CRD)?
Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!
北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
Software testing dry goods
DoD 和 DoR,消减「认知偏差」的两大神器
为什么应该在开发环境中使用 Kubernetes
周鸿祎:360是世界上最大的安全大数据公司
Applied practical skills of deep reinforcement learning
面试中项目讲解的步骤
QT工程基本构建
Survival analysis using rtcga clinical data
ggdag 绘制DAG和因果图
R 语言 用黎曼和求近似 积分
Review of the 16th issue of HMS core discovery | play with the new "sound" state of AI with tiger pier
The 2022 open atom global open source summit opened in Beijing