C. Petya and Exam---------思维/贪心

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 n problems that can be solved in T minutes. Thus, the exam begins at time 0 and ends at time T. Petya can leave the exam at any integer time from 0 to T, inclusive.

All problems are divided into two types:

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

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

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

if he leaves at time s=0, then he will receive 0 points since he will not have time to solve any problems;
if he leaves at time s=1, he will receive 0 points since he will not have time to solve any problems;
if he leaves at time s=2, then he can get a 1 point by solving the problem with the number 2 (it must be solved in the range from 0 to 2);
if he leaves at time s=3, then he will receive 0 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=4, then he will receive 0 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=5, then he can get 2 points by solving all problems.
Thus, the answer to this test is 2.

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

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

The next lines contain a description of m test cases.

The first line of each test case contains four integers n,T,a,b (2≤n≤2⋅105, 1≤T≤109, 1≤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 n numbers 0 or 1, separated by single space: the i-th number means the type of the i-th problem. A value of 0 means that the problem is easy, and a value of 1 that the problem is hard.

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

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

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

Example
inputCopy

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
outputCopy
3
2
1
0
1
4
0
1
2
1

题意:有一个人参加考试,考试只有两种题,一种是简单题,每道题耗时固定为a;另一种是困难题,每道题耗时固定为b,保证b>a。解出一道题得分都是1。考试的规则并不只是写多少题得多少分,鼓励提前交卷。假如你没有提前交卷,那么有一部分的题目会列为“必做题”,当“必做题”的题目没有全部被完成的话,这门课就算0分;否则得到与题数相同的分数,包括“必做”和“非必做”的。.

特别注意的是:注意,离开Petya可以同时解决“强制”和“非强制”问题。(什么意思呢?就是解决强制问题后,还有剩余时间可以把非强制的问题给做了) (一开始没注意到一直错)

解析:我们按照“必做时间”升序排序,然后贪心,从头开始遍历每道题目的必做时间。假设遍历到第i题,当前时间为T,那如果在T-1时刻交卷。首先我们需要把前面的题目都做完,假设这个时间为Ti,那么所剩时间为T-1-Ti.我们在剩下的时间里贪心去做简单题,再做难题。记录ans,不断往后遍历,不断的更新答案ans。


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10000;
typedef long long ll;
struct node
{
	int val;
	int t;
}v[N];
int m,n,t,a,b;
bool cmp(const node &a,const node &b)
{
	if(a.t==b.t) return a.val<b.val;
	return a.t<b.t;
}
int main()
{
	cin>>m;
	while(m--)
	{
		cin>>n>>t>>a>>b;
		ll cnta=0,cntb=0;
		for(int i=1;i<=n;i++)
		{
			cin>>v[i].val;
			if(v[i].val==0) cnta++;  //记录简单题数目
			else cntb++;//记录难题数目
		}
		for(int i=1;i<=n;i++) cin>>v[i].t;
		sort(v+1,v+1+n,cmp);
		ll ca=0,cb=0; //ca,cb分别记录前面所做题的简单题和难题数目
		v[n+1].t=t+1;
		ll ans=0;
		for(int i=1;i<=n+1;i++)
		{
			ll cur=a*ca+b*cb;//必做题目花费的时间 
			ll time=v[i].t-1-cur; //所剩的时间
			if(time>=0)
			{
				ll ta=min(time/a,cnta-ca);  
				time-=ta*a;
				ll tb=min(time/b,cntb-cb);
				ans=max(ans,ca+cb+ta+tb);//之前所做的题目数ca+cb,剩下时间所做的题目数ta+tb
			 } 
			if(v[i].val==0) ca++;
			else cb++;
		}
		cout<<ans<<endl;
	}
}



发布了289 篇原创文章 · 获赞 6 · 访问量 4005

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104062632