本文主要是介绍Codeforces Contest 1070 problem E Getting Deals Done —— 二分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Polycarp has a lot of work to do. Recently he has learned a new time management rule: “if a task takes five minutes or less, do it immediately”. Polycarp likes the new rule, however he is not sure that five minutes is the optimal value. He supposes that this value d should be chosen based on existing task list.
Polycarp has a list of n tasks to complete. The i-th task has difficulty pi, i.e. it requires exactly pi minutes to be done. Polycarp reads the tasks one by one from the first to the n-th. If a task difficulty is d or less, Polycarp starts the work on the task immediately. If a task difficulty is strictly greater than d, he will not do the task at all. It is not allowed to rearrange tasks in the list. Polycarp doesn’t spend any time for reading a task or skipping it.
Polycarp has t minutes in total to complete maximum number of tasks. But he does not want to work all the time. He decides to make a break after each group of m consecutive tasks he was working on. The break should take the same amount of time as it was spent in total on completion of these m tasks.
For example, if n=7, p=[3,1,4,1,5,9,2], d=3 and m=2 Polycarp works by the following schedule:
Polycarp reads the first task, its difficulty is not greater than d (p1=3≤d=3) and works for 3 minutes (i.e. the minutes 1, 2, 3);
Polycarp reads the second task, its difficulty is not greater than d (p2=1≤d=3) and works for 1 minute (i.e. the minute 4);
Polycarp notices that he has finished m=2 tasks and takes a break for 3+1=4 minutes (i.e. on the minutes 5,6,7,8);
Polycarp reads the third task, its difficulty is greater than d (p3=4>d=3) and skips it without spending any time;
Polycarp reads the fourth task, its difficulty is not greater than d (p4=1≤d=3) and works for 1 minute (i.e. the minute 9);
Polycarp reads the tasks 5 and 6, skips both of them (p5>d and p6>d);
Polycarp reads the 7-th task, its difficulty is not greater than d (p7=2≤d=3) and works for 2 minutes (i.e. the minutes 10, 11);
Polycarp notices that he has finished m=2 tasks and takes a break for 1+2=3 minutes (i.e. on the minutes 12,13,14).
Polycarp stops exactly after t minutes. If Polycarp started a task but has not finished it by that time, the task is not considered as completed. It is allowed to complete less than m tasks in the last group. Also Polycarp considers acceptable to have shorter break than needed after the last group of tasks or even not to have this break at all — his working day is over and he will have enough time to rest anyway.
Please help Polycarp to find such value d, which would allow him to complete maximum possible number of tasks in t minutes.
Input
The first line of the input contains single integer c (1≤c≤5⋅104) — number of test cases. Then description of c test cases follows. Solve test cases separately, test cases are completely independent and do not affect each other.
Each test case is described by two lines. The first of these lines contains three space-separated integers n, m and t (1≤n≤2⋅105,1≤m≤2⋅105,1≤t≤4⋅1010) — the number of tasks in Polycarp’s list, the number of tasks he can do without a break and the total amount of time Polycarp can work on tasks. The second line of the test case contains n space separated integers p1,p2,…,pn (1≤pi≤2⋅105) — difficulties of the tasks.
The sum of values n for all test cases in the input does not exceed 2⋅105.
Output
Print c lines, each line should contain answer for the corresponding test case — the maximum possible number of tasks Polycarp can complete and the integer value d (1≤d≤t) Polycarp should use in time management rule, separated by space. If there are several possible values d for a test case, output any of them.
Examples
inputCopy
4
5 2 16
5 6 1 4 7
5 3 30
5 6 1 4 7
6 4 15
12 5 15 7 20 17
1 1 50
100
outputCopy
3 5
4 7
2 10
0 25
inputCopy
3
11 1 29
6 4 3 7 5 3 4 7 3 5 3
7 1 5
1 1 1 1 1 1 1
5 2 18
2 3 3 7 5
outputCopy
4 3
3 1
4 5
Note
In the first test case of the first example n=5, m=2 and t=16. The sequence of difficulties is [5,6,1,4,7]. If Polycarp chooses d=5 then he will complete 3 tasks. Polycarp will work by the following schedule:
Polycarp reads the first task, its difficulty is not greater than d (p1=5≤d=5) and works for 5 minutes (i.e. the minutes 1,2,…,5);
Polycarp reads the second task, its difficulty is greater than d (p2=6>d=5) and skips it without spending any time;
Polycarp reads the third task, its difficulty is not greater than d (p3=1≤d=5) and works for 1 minute (i.e. the minute 6);
Polycarp notices that he has finished m=2 tasks and takes a break for 5+1=6 minutes (i.e. on the minutes 7,8,…,12);
Polycarp reads the fourth task, its difficulty is not greater than d (p4=4≤d=5) and works for 4 minutes (i.e. the minutes 13,14,15,16);
Polycarp stops work because of t=16.
In total in the first test case Polycarp will complete 3 tasks for d=5. He can’t choose other value for d to increase the number of completed tasks.
题意:
给你n道题,每道题都有一个难度,解决它需要花掉相应难度的时间,做m道题目需要休息这m题累计和的时间,但是最后一次不需要,你总共有t的时间,假设你会做难度为d的题目,那你一旦遇到一道难度不超过d的题目,你就会把它做出来,问你你最多能做多少题目,以及做的题目难度不超过多少(d有多种解,输出任意一种)
题解:
我们不能二分难度,因为它不符合二分的性质:你将难度调小,可能做出比现在少的题目,你将难度调大,也可能做出比现在少的题目,所以只能二分做的题目数量,而这个是符合二分性质的,因为做的题目越多,需要的时间肯定就越大,想到了这一点,这道题就做出来了
check中,tim表示用了多少时间,sum表示在一个m中,需要多少时间,cnt表示能做的题目的数量,由于同一难度的题目数量不止一个,那么我们就要在for的时候判断是否已经可以。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll a[N],b[N];
int n,m;
ll t;
bool check(int x)
{int up=b[x];ll tim=0,sum=0,cnt=0;for(int i=1;i<=n;i++){if(cnt%m==0&&cnt!=0&&cnt!=x)tim+=sum*2,sum=0;if(tim>t)return 0;if(a[i]<=up){cnt++;sum+=a[i];}if(cnt>=x&&tim+sum<=t)return 1;}if(tim+sum>t)return 0;return 1;
}
int main()
{int c;scanf("%d",&c);while(c--){scanf("%d%d%lld",&n,&m,&t);for(int i=1;i<=n;i++)scanf("%lld",&a[i]),b[i]=a[i];sort(b+1,b+1+n);int l=0,r=n,mid,ans;while(r>=l){mid=l+r>>1;if(check(mid))ans=mid,l=mid+1;elser=mid-1;}printf("%d %lld\n",ans,b[ans]==0?1:b[ans]);}return 0;
}
这篇关于Codeforces Contest 1070 problem E Getting Deals Done —— 二分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!