当前位置:网站首页>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;
}
边栏推荐
- Leetcode binary tree series -- 144. Preorder traversal of binary trees
- LeetCode二叉树系列——144.二叉树的前序遍历
- 基于flask写的一个小商城mall项目
- 就这?TypeScript其实并不难!(建议收藏)
- Spark高效数据分析02、基础知识13篇
- js两个数组对象进行合并去重
- PHP basics uses arrays to save data
- Zhou Hongyi: 360 is the largest secure big data company in the world
- 聊聊性能测试环境搭建
- ggdag 绘制DAG和因果图
猜你喜欢

ES6 arrow function this points to

VMware: use commands to update or upgrade VMware esxi hosts

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

学习R语言这几本电子书就够了!

站点数据收集-Scrapy使用笔记

Understand what a binary tree is (types, traversal methods, definitions of binary trees)

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

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

If distributed file storage is realized according to integrated Minio

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
随机推荐
LeetCode_ 278_ First wrong version
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Kunlunbase instruction manual (III) data import & synchronization
建议收藏丨sql行转列的一千种写法!!
站点数据收集-Scrapy使用笔记
Niuke net brush questions
One click blog building: how to use WordPress plug-in to build a dedicated blog
Svn revision keyword
PHP basics uses arrays to save data
使用R包skimr汇总统计量的优美展示
leetcode-位运算
[unity, C #] character keyboard input steering and rotation
使用 RTCGA 临床数据进行生存分析
Error: Protobuf syntax version should be first thing in file
DNS协议、ICMP协议、NAT技术
Hugo NexT V4 介绍
主子仓库都修改,如何进行同步?
牛客网刷题
一键搭建博客:如何使用WordPress插件搭建专属博客
Kunlunbase instruction manual (II) best practices for peer-to-peer deployment