Codeforces Round #610 (Div. 2) C. Petya and Exam
链接:
https://codeforces.com/contest/1282/problem/C
题意:
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.
思路:
枚举每个逃脱点,计算之前的值,剩下的从后往前取时间用的少的
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
struct Node
{
int lim, lev;
bool operator < (const Node& rhs) const
{
return this->lim < rhs.lim;
}
}node[MAXN];
int n, t, a, b;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d", &n, &t, &a, &b);
int cnte = 0, cnth = 0;
for (int i = 1;i <= n;i++)
{
scanf("%d", &node[i].lev);
node[i].lev ? cnth++ : cnte++;
}
for (int i = 1;i <= n;i++)
scanf("%d", &node[i].lim);
sort(node+1, node+1+n);
int ans = 0;
LL sumt = 0;
int p1 = 0, p2 = 0;
while(p1 <= n && p2 <= n)
{
while(node[p1].lim == node[p2].lim && p2 <= n)
{
if (p2 != 0)
sumt += node[p2].lev ? b : a, node[p2].lev ? cnth-- : cnte--;
p2++;
}
int up = p2 > n ? t : node[p2].lim-1;
if (up >= sumt)
{
LL les = up-sumt;
int tmp = min(cnte, (int)les/a);
les -= tmp*a;
tmp += min(cnth, (int)les/b);
ans = max(ans, p2-1+tmp);
}
p1 = p2;
}
printf("%d\n", ans);
}
return 0;
}