2022 杭电多校3 I - Package Delivery (贪心)

input:

1
4 2
1 3
2 4
6 7
4 7

output:

2

题目大意:给我们n个区间,我们可以选择一个端点,将端点所在的区间给拿走,但是我们一次最多拿k个区间,问最少拿多少次可以将所有区间拿完。

解题思路:上自己比赛时手写的思路,字迹较丑,不喜勿喷。

 上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N=1E5+10;
int n,k;
struct node {
	int l; 
	int r;
	int pos;
}p1[N],p2[N];
bool cmp1(struct node a,struct node b)
{
	return a.r<b.r;
}
bool cmp2(struct node a,struct node b)
{
	return a.l<b.l;
 }
struct cmp{
	bool operator()(const node a,const node b)const
	{
		return a.r>b.r;
	}
}; 
bool vis[N];
int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0); 
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		memset(vis,false,sizeof vis);
		for(int i=1;i<=n;i++)
		{
			cin>>p1[i].l>>p1[i].r;
			p2[i].l=p1[i].l;
			p2[i].r=p1[i].r;
			p1[i].pos=p2[i].pos=i;
		}
		sort(p1+1,p1+n+1,cmp1);
		sort(p2+1,p2+n+1,cmp2);
		int ans=0;
		priority_queue<node,vector<node>,cmp >q;
		int tag=0;
		int cnt=0;
		for(int i=1,j=1;i<=n;i++)
		{
			if(!vis[p1[i].pos])
			{
				while(j<=n&&p2[j].l<=p1[i].r)
				{
					q.push(p2[j]);
					j++;
				}
				ans++;
				for(int x=1;x<=k;x++)
				{
					if(q.empty())
					break;
					vis[q.top().pos]=true;
					q.pop();
				}
		 	}
		}
		cout<<ans<<endl; 
	}	
	return 0;
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值