codeforces-gym-100187-F【贪心】

本文介绍了一种解决末日求生问题的算法。在限定时间内,人们需找到最近的避难所并尽可能多地生存下来。通过贪心策略,文章详细阐述了如何最大化生存人数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

F. Doomsday
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmk and t (1 ≤ n, m, k ≤ 2000001 ≤ 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.

Examples
input
2 2 1 5
45 55
40 60
output
2
input
2 2 1 5
45 54
40 60
output
1
input
2 2 2 5
45 35
40 60
output
2
input
3 3 1 5
40 45 45
45 50 50
output
3


大意:世界末日在 t 秒后要来了,n 个人,m 个避难所,一个避难所最多容纳 k 个人,每个人一秒只能走 1 米,给出所有人,所有避难所的位置(都位于同一直线上),问最后最多几个人可以活下来。


思路:就是贪心,一直挂在 test11 上,后来改了才过。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,t;
int a[200010],b[200010];
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&k,&t))
	{
		for(int i=0;i<n;i++)
			scanf("%d",a+i);
		for(int i=0;i<m;i++)
			scanf("%d",b+i);
		sort(a,a+n);
		sort(b,b+m);
		int ans=0,j=0;
		for(int i=0;i<m;i++)
		{
			int v=k;
			while(j<n&&a[j]-t<=b[i]&&v) // 这里注意一下,只要人往左走最大能走到避难所的左边,且此时避难所有空位置,那么不管这个人能不能进去,都去换下一个人上 
			{

				if(a[j]+t>=b[i])
				{
					ans++;
					v--;	
				}
				j++;
			}
			if(j==n)	break;
		}
		printf("%d\n",ans);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值