当前位置:网站首页>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;
}
边栏推荐
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- TCP和UDP
- 使用tidymodels搞定二分类logistic模型
- Using xgboost with tidymodels
- Pytorch 入门
- R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
- Use R-Pack skimr to collect the beautiful display of President measurement
- Leetcode binary tree series -- 144. Preorder traversal of binary trees
- If distributed file storage is realized according to integrated Minio
- 建议收藏丨sql行转列的一千种写法!!
猜你喜欢

If distributed file storage is realized according to integrated Minio

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

Site data collection -scrapy usage notes

开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览

Learning R language these ebooks are enough!

Detailed arrangement of JVM knowledge points (long text warning)

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

The 2022 open atom global open source summit opened in Beijing

周鸿祎:360是世界上最大的安全大数据公司

LeetCode二叉树系列——144.二叉树的前序遍历
随机推荐
8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
R package pedquant realizes stock download and financial quantitative analysis
『知识集锦』一文搞懂mysql索引!!(建议收藏)
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
Kunlunbase instruction manual (III) data import & synchronization
PaddleLite 编译以及代码跑通复盘
R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
重磅 | 基金会为白金、黄金、白银捐赠人授牌
美团、饿了么被杭州市监约谈要求落实食品安全管理责任 严禁恶意竞争
LeetCode_416_分割等和子集
Spark高效数据分析01、idea开发环境搭建
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
使用R包skimr汇总统计量的优美展示
大伟 Golang之路
Discussion on the application of arcing smart electricity in elderly care institutions
自采集在线电脑壁纸php源码v2.0自适应端
TCP和UDP
DOD and Dor, two artifacts to reduce "cognitive bias"
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码