C. Petya and Exam Codeforces Round #610 (Div. 2)c (贪心)

C. Petya and Exam Codeforces Round #610 (Div. 2)c (贪心)

C. Petya and Exam

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.

The exam consists of nn problems that can be solved in TT minutes. Thus, the exam begins at time 00 and ends at time TT. Petya can leave the exam at any integer time from 00 to TT, inclusive.

All problems are divided into two types:

easy problems — Petya takes exactly aa minutes to solve any easy problem;
hard problems — Petya takes exactly bb minutes (b>ab>a) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time xx, then it will be solved at time x+ax+a. Similarly, if at a time xx Petya starts to solve a hard problem, then it will be solved at time x+bx+b.

For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time titi (0≤ti≤T0≤ti≤T) at which it will become mandatory (required). If Petya leaves the exam at time ss and there is such a problem ii that ti≤sti≤s and he didn’t solve it, then he will receive 00 points for the whole exam. Otherwise (i.e if he has solved all such problems for which ti≤sti≤s) he will receive a number of points equal to the number of solved problems. Note that leaving at time ss Petya can have both “mandatory” and “non-mandatory” problems solved.

For example, if n=2n=2, T=5T=5, a=2a=2, b=3b=3, the first problem is hard and t1=3t1=3 and the second problem is easy and t2=2t2=2. Then:

if he leaves at time s=0s=0, then he will receive 00 points since he will not have time to solve any problems;
if he leaves at time s=1s=1, he will receive 00 points since he will not have time to solve any problems;
if he leaves at time s=2s=2, then he can get a 11 point by solving the problem with the number 22 (it must be solved in the range from 00 to 22);
if he leaves at time s=3s=3, then he will receive 00 points since at this moment both problems will be mandatory, but he will not be able to solve both of them;
if he leaves at time s=4s=4, then he will receive 00 points since at this moment both problems will be mandatory, but he will not be able to solve both of them;
if he leaves at time s=5s=5, then he can get 22 points by solving all problems.
Thus, the answer to this test is 22.

Help Petya to determine the maximal number of points that he can receive, before leaving the exam.

Input

The first line contains the integer mm (1≤m≤1041≤m≤104) — the number of test cases in the test.

The next lines contain a description of mm test cases.

The first line of each test case contains four integers n,T,a,bn,T,a,b (2≤n≤2⋅1052≤n≤2⋅105, 1≤T≤1091≤T≤109, 1≤a<b≤1091≤a<b≤109) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.

The second line of each test case contains nn numbers 00 or 11, separated by single space: the ii-th number means the type of the ii-th problem. A value of 00 means that the problem is easy, and a value of 11 that the problem is hard.

The third line of each test case contains nn integers titi (0≤ti≤T0≤ti≤T), where the ii-th number means the time at which the ii-th problem will become mandatory.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

Print the answers to mm test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam.

Example

input

Copy

10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
output

Copy

3
2
1
0
1
4
0
1
2
1

题意:

给了n道题,T分钟。难题用时b分钟,简单题用时a分钟。并且每道题规定在 t i t_i ti分钟及之前动笔。在没有开下一道题之前我们可以跑路。问我们可以最多做出多少题。

题解:

我们可以想到贪心,并且先完成限制时间小的题目。我们这里将两两题目之间解题分成一种情况,比如我们开了第三题则证明我们前两题是已经完成了的,这个时候我们只用考虑我们 在第三题和第四题之间这块时间我们可以完成多少道题目,因为想使每块时间最大利用,所以我们肯定先开简单题,然后剩下的时间再贪心做难题。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+7;
struct Node
{
	int ishard,limit,time;
}pro[N];
int cmp(Node a,Node b)
{
	if(a.limit!=b.limit) return a.limit<b.limit;
}
signed main()
{
	int t; cin>>t;
	while(t--){
		int n,T,a,b,easy=0,hard=0; scanf("%lld%lld%lld%lld",&n,&T,&a,&b);
		for(int i=1;i<=n;i++){
			scanf("%lld",&pro[i].ishard);
			if(pro[i].ishard==1) hard++,pro[i].time=b;
			else easy++,pro[i].time=a;
		}
		for(int i=1;i<=n;i++) scanf("%lld",&pro[i].limit);
		pro[n+1].limit=T+1;
		sort(pro+1,pro+1+n,cmp);
		int sum=0,ans=0;
		for(int i=0;i<=n;i++){
			if(i!=0){
				sum+=pro[i].time;
				if(pro[i].ishard==1) hard--;
				else easy--;
			}
			if((pro[i].limit!=pro[i+1].limit||i==0||i==n)&&sum<=T&&sum<pro[i+1].limit){
				int res=pro[i+1].limit-sum-1;
				int num1=min(easy,res/a);
				res-=num1*a;
				int num2=min(hard,res/b);
				ans=max(ans,num1+num2+i);
			}
		}
		printf("%lld\n",ans);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解法:贪心算法 如果两个字符串不相等,那么它们必定至少有一位不一样。考虑对于这一位,我们应该对字符串 a 进行哪种操作,才能使得它更接近于字符串 b。 首先,我们可以通过交换字符串 a 中的两个数字,使得这一位变为我们想要的数字。如果我们把这一位变成了 b 中的数字,那么显然这一位就不需要再进行修改了。因此,我们只需要考虑把这一位变成 a 中的数字 4 或 7。 如果我们把这一位变成 a 中的数字,则需要执行一次操作;如果我们把这一位变成 a 中的数字,则需要执行一次操作。那么,我们应该采取哪种操作呢? 我们可以贪心地想,如果我们把这一位变成 a 中的数字,那么这一位和 b 中的数字就越相似,那么接下来的操作就越容易执行。因此,我们应该选择将这一位变成 a 中的数字,从而尽可能地增加和 b 相同的数字的数量。 实现时,我们可以从左到右扫描字符串 a 和 b,统计它们不同的位置的数量。对于每个不同的位置,我们都可以选择将这一位变成 4 或 7,然后更新 a 中数字 4 和 7 的数量。最终,我们就可以得到将字符串 a 转换为字符串 b 所需的最少操作数。 时间复杂度 字符串 a 和 b 的长度为 n,我们需要扫描一遍字符串并统计数字 4 和 7 的数量,因此时间复杂度为 O(n)。 空间复杂度 我们只需要存储数字 4 和 7 的数量,因此空间复杂度为 O(1)。 Python 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值