当前位置:网站首页>(2022 Hangdian multi school III) 1009.package delivery (greedy)
(2022 Hangdian multi school III) 1009.package delivery (greedy)
2022-07-27 07:22:00 【AC__ dream】
subject :
The sample input :
1
4 2
1 3
2 4
6 7
4 7Sample output :
2
The question : Multiple tests , For each set of tests , Give me a n and k,n Represents the number of express delivery ,k Represents the maximum number of express deliveries we can get at one time , Next n Each line gives two numbers , Respectively represents the arrival time and deadline of express , We have to pick up the express before the deadline , You can get multiple express deliveries in a day , Ask us how many times we need to pick up all express delivery at least .
analysis : This is obviously a greedy problem , Since there is no limit to the number of times you can pick up express delivery in a day , Then we can try to postpone the time of picking up the express , In this way, our express delivery number will be more and more , Accordingly, the number of express deliveries taken out at one time may also be more , We only pick up express delivery at the deadline , We should pick up as many express deliveries as possible at one time , First of all, we need to follow l Ascending order , Then traverse from front to back , We can set a time t Represents our current time point , We use a queue to record the express that has arrived at the current time and has not been taken out , Then all l<t All express deliveries should be added to the queue ( The express that hasn't been taken out ), This queue follows r Ascending order , Because when t Equal to the deadline of an express delivery, we must take it out once .
When t There are two situations for the number of couriers in the queue when the deadline for a courier arrives :
(1) Less than or equal to k individual , Then we can take it out directly
(2) Greater than k individual , Then we will r Arrive and take out from childhood k Just one ( Even if it's not finished, just take k individual )
The reason why the number of express delivery is greater than k The reason why we didn't take it all in one hour was that we took the urgent express delivery first , Then if the remaining Express has not arrived at the deadline , We can wait a little , Maybe you can take it out with other later express delivery , The answer may be better
This is the greedy strategy of this topic , See code for details :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=400010;
typedef pair<int,int> PII;
int l[N],r[N];
struct node{
int l,r;
}p[N];
bool cmp(node a,node b)
{
return a.l<b.l;
}
void solve()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].l,&p[i].r);
sort(p+1,p+n+1,cmp);
priority_queue<PII,vector<PII>,greater<PII> >q;
while(!q.empty()) q.pop();
q.push({p[1].r,p[1].l});// First put r You can avoid handwriting sorting functions
int t=p[1].r,ans=0;
for(int i=2;i<=n;)
{
while(i<=n&&(p[i].l<=t||t==-1))
{
if(t==-1) t=p[i].r;
t=min(t,p[i].r);
q.push({p[i].r,p[i].l});
i++;
}
if(!q.empty())
{
ans++;
if(q.size()<=k)
{
while(!q.empty()) q.pop();
t=-1;
}
else
{
int tt=k;
while(tt--) q.pop();
t=q.top().first;
}
}
}
ans+=(q.size()+k-1)/k;// Finally, don't forget to take out all express delivery
printf("%d\n",ans);
}
int main()
{
int _;
cin>>_;
while(_--) solve();
return 0;
}边栏推荐
- Algorithm -- Fibonacci sequence (kotlin)
- Convert Excel to csv/csv UTF-8
- docker安装MySQL8.0.28
- VIM editor deletes all file contents
- A Competitive Swarm Optimizer for Large Scale Optimization
- 软件测试十大必问面试题(附答案和解析)
- jjwt 生成token
- (转帖)eureka、consul、nacos的对比1
- tableau prep连接maxcompute,只是书写很简单的sql,为啥报这个错误呢?
- DRConv-pytorch改称输出和输入一样的尺寸
猜你喜欢

DDD Domain Driven Design Notes

Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.

Synchronized锁

Pytorch notes: td3

一款开源 OA 办公自动化系统

从技术原理看元宇宙的可能性:Omniverse如何“造”火星

Internal class -- just read this article~

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

网络入门——vlan及trunk概述

指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
随机推荐
Chapter 6 Shell Logic and Arithmetic
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
把Excel转换成CSV/CSV UTF-8
单臂路由(讲解+实验)
MySQL: 提高最大连接数
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
Word wrap: break word line feed is compatible with browsers
C4D动画如何提交云渲染农场快速渲染?
Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
MySQL quickly compares database table data
2022-07-25 Gu Yujia's study notes
Algorithm -- Fibonacci sequence (kotlin)
使用反射实现动态修改@Excel的注解属性
LogCat工具
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
MySQL2
【QT】无法在QT创建者中打开包含文件pcap.h(C1083)
Logcat tool
MySQL optimization SQL related (continuous supplement)
Linear table -- stack and queue