Gym 100187F (贪心+覆盖)

Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will be possible to survive. But each vault can accommodate only k people and each person can pass only one unit of distance per one unit of time. Fortunately, all people and vaults are now on the straight line, so there is no confusion and calculations should be simple.

You are given the positions of the people and the vaults on the line. You are to find the maximal number of people who can hide in vaults and think they will survive.

Input 
The first line contains four integers n, m, k and t (1 ≤ n, m, k ≤ 200000, 1 ≤ t ≤ 109) separated by spaces — the number of people, the number of vaults, the capacity of one vault and the time left to the Doomsday.

The second line contains n integers separated by spaces — the coordinates of the people on the line.

The third line contains m integers separated by spaces — the coordinates of the vaults on the line.

All the coordinates are between  - 109 and 109, inclusively.

Output 
Output one integer — the maximal number of people who can hide in vaults and think they will survive.

Example 
Input 
2 2 1 5 
45 55 
40 60 
Output 

Input 
2 2 1 5 
45 54 
40 60 
Output 

Input 
2 2 2 5 
45 35 
40 60 
Output 

Input 
3 3 1 5 
40 45 45 
45 50 50 
Output 

3

题意:世界末日要来了,有n个人,m个地下室,每个地下室可以有k个人,共有t个时间单位,每个单位可以走一个格,都在一条直线上,问最多有多少能活下来。

首先对人和地下室先从小到大排序,然后考虑每个地下室,从1-m个,然后考虑人的情况,当人在范围里面总和就加1

 

#include<cstdio> 
#include<cstring>
#include<algorithm>
#define N 200010
using namespace std;
int num[N];
int val[N];  //地下室
int main()
{
	int n,m,k,t;
	scanf("%d%d%d%d",&n,&m,&k,&t);
	for(int i=0;i<n;i++)
	   scanf("%d",&num[i]);
	for(int i=0;i<m;i++)
		scanf("%d",&val[i]);
	sort(num,num+n);
	sort(val,val+m);
	int sum=0;
	int j=0;
	for(int i=0;i<m;i++)
	{
		int flag=k;
		for(;num[j]<=val[i]+t && j<n && flag;j++)
		{
			if(num[j]>=val[i]-t)
			{
				flag--;
			    sum++;
			}		
		} 
		if(j==n)
		    break;
	}
	printf("%d\n",sum);
	return 0;
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值