codeforces day 3(一个明显的贪心思想,疫苗包的开封)

该问题涉及疫苗接种策略优化,目标是确定在给定条件下(疫苗包剂量数、有效期、患者等待时间等)接种所有患者所需的最少疫苗包数量。采用贪心算法,根据患者到达时间和疫苗有效期来决定何时开启新的疫苗包,确保最大化利用每个包。

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

B. Vaccination

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Ethan runs a vaccination station to help people combat the seasonal flu. He analyses the historical data in order to develop an optimal strategy for vaccine usage.

Consider there are nn patients coming to the station on a particular day. The ii-th patient comes at the moment titi. We know that each of these patients can be asked to wait for no more than ww time moments. That means the ii-th patient can get vaccine at moments ti,ti+1,…,ti+wti,ti+1,…,ti+w.

Vaccines come in packs, each pack consists of kk doses. Each patient needs exactly one dose. Packs are stored in a special fridge. After a pack is taken out of the fridge and opened, it can no longer be put back. The lifetime of the vaccine outside the fridge is dd moments of time. Thus, if the pack was taken out of the fridge and opened at moment xx, its doses can be used to vaccinate patients at moments x,x+1,…,x+dx,x+1,…,x+d. At moment x+d+1x+d+1 all the remaining unused doses of this pack are thrown away.

Assume that the vaccination station has enough staff to conduct an arbitrary number of operations at every moment of time. What is the minimum number of vaccine packs required to vaccinate all nn patients?

Input

The first line of the input contains the number of test cases tt (1≤t≤1041≤t≤104). Then follow tt descriptions of the test cases.

The first line of each test case contains four integers nn, kk, dd and ww (1≤n,k≤2⋅1051≤n,k≤2⋅105, 0≤d,w≤1060≤d,w≤106). They are the number of patients, the number of doses per vaccine pack, the number of moments of time the vaccine can live outside the fridge, and the number of moments of time each of the patients can wait, respectively.

The second line of each test case contains a non-decreasing sequence t1,t2,…,tnt1,t2,…,tn (0≤t1≤t2≤…≤tn≤1060≤t1≤t2≤…≤tn≤106). The ii-th element of this sequence is the moment when the ii-th patient comes to the vaccination station.

It is guaranteed that the sum of nn over all test cases won't exceed 2⋅1052⋅105.

Output

Output one integer, the minimum number of vaccine packs required to vaccinate all nn patients.

Example

input

Copy


5

6 3 5 3

1 2 3 10 11 18

6 4 0 0

3 3 3 3 3 4

9 10 2 2

0 1 2 3 4 5 6 7 8

3 10 3 6

10 20 30

5 5 4 4

0 2 4 6 8

output

Copy

2
3
2
3
1

Note

In the first example, the first pack can be opened at moment 11 to vaccinate patient 11. The vaccine is durable enough to be used at moments 22 and 33 for patients 22 and 33, respectively. Then the staff needs to ask patients 44 and 55 to wait for moment 1313. At moment 1313 the staff opens the second vaccine pack and serves patients 44 and 55. Finally, the last patient comes at moment 1818 and immediately gets the last dose of the second pack while it is still fine.

In the second example, the vaccine should be used exactly at the moment it is taken out of the fridge. Moreover, all the patients want to be served at exactly the same moment they come. That means the staff needs to open two packs at moment 33 and use five doses on patients 11, 22, 33, 44, and 55. There will be three doses left ouf of these two packs but they can't be used for patient 66. When patient 66 comes at moment 44 the staff needs to open a new pack just to use only one dose out of it.

分析题意可得,我们首先要满足两个前提:一是要让所有病人都能够打到疫苗,二是让使用得到的疫苗包尽可能的少。

那么,我们要满足一个什么样的条件呢。

那就是让每一个疫苗包都尽可能的让更多的人使用的到。

于是,我们很容易想到贪心。

对于每一个疫苗包都利用局部最优的想法来解决。

首先分为两种情况:第一就是在一个人来时这时已经有一个被打开的疫苗包,那么在小于等于k的情况下立即使用。

二是,在这个人到达时候,并没有被打开的疫苗包,那么,这时我们要立即打开一个疫苗包吗?很明显不是,让我们等待这个人到达忍耐度最大w时,看一看这期间会不会有新的人到达,这样就可以最大化利用疫苗包。

当然,我们也一定要加上相应的限制条件。

比如,疫苗包的使用时间之类的。

很详细的注释,希望能帮助到大家。

#include<iostream>
using namespace std;
int main()
{
//关闭同步流,这是和学长新学到的知识,避免我们被scanf的繁琐步骤所烦心。
	std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    int m;
    std::cin>>m;
    while(m--)
    {
    	int n,k,d,w;
    	std::cin>>n>>k>>d>>w;//得到相应的数据
    	int use=0;//记录这时候我们已经用了几个药剂
    	int last =0;//记录上一次的时间
    	int ans=0;//药剂包的总次数
    	for(int i=0;i<n;i++)//对每一个患者都进行贪心思想求取其值
    	{
    		int t;
    		std::cin>>t;
    		if(!use||t>=last+d+1)//如果当前没有开封的药剂包,立即开一个。如果这个人到达的时间大于等于药剂开封的时间且加一,加一是因为患者正好到达也能打针。
    		{
    			use=1;//开封,将use的值覆盖为一
    			last=t+w;//因为我们每一次开封都要等到这个人快不行时候才开,所以last=t+w
    			ans++;
    			 if (use == k) {//防止K=1的情况,我就白白wa了一次。
                use = 0;
            }
			}
			else
			{
				use++;
				if(use==k)use=0;
			}
		}
    	 std::cout << ans << "\n";
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值