zoukankan      html  css  js  c++  java
  • codeforce 1070 E. Getting Deals Done

    E. Getting Deals Done
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 dd should be chosen based on existing task list.

    Polycarp has a list of nn tasks to complete. The ii -th task has difficulty pipi , i.e. it requires exactly pipi minutes to be done. Polycarp reads the tasks one by one from the first to the nn -th. If a task difficulty is dd or less, Polycarp starts the work on the task immediately. If a task difficulty is strictly greater than dd , 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 tt 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 mm 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 mm tasks.

    For example, if n=7n=7 , p=[3,1,4,1,5,9,2]p=[3,1,4,1,5,9,2] , d=3d=3 and m=2m=2 Polycarp works by the following schedule:

    • Polycarp reads the first task, its difficulty is not greater than dd (p1=3d=3p1=3≤d=3 ) and works for 33 minutes (i.e. the minutes 11 , 22 , 33 );
    • Polycarp reads the second task, its difficulty is not greater than dd (p2=1d=3p2=1≤d=3 ) and works for 11 minute (i.e. the minute 44 );
    • Polycarp notices that he has finished m=2m=2 tasks and takes a break for 3+1=43+1=4 minutes (i.e. on the minutes 5,6,7,85,6,7,8 );
    • Polycarp reads the third task, its difficulty is greater than dd (p3=4>d=3p3=4>d=3 ) and skips it without spending any time;
    • Polycarp reads the fourth task, its difficulty is not greater than dd (p4=1d=3p4=1≤d=3 ) and works for 11 minute (i.e. the minute 99 );
    • Polycarp reads the tasks 55 and 66 , skips both of them (p5>dp5>d and p6>dp6>d );
    • Polycarp reads the 77 -th task, its difficulty is not greater than dd (p7=2d=3p7=2≤d=3 ) and works for 22 minutes (i.e. the minutes 1010 , 1111 );
    • Polycarp notices that he has finished m=2m=2 tasks and takes a break for 1+2=31+2=3 minutes (i.e. on the minutes 12,13,1412,13,14 ).

    Polycarp stops exactly after tt 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 mm 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 dd , which would allow him to complete maximum possible number of tasks in tt minutes.

    Input

    The first line of the input contains single integer cc (1c51041≤c≤5⋅104 ) — number of test cases. Then description of cc 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 nn , mm and tt (1n2105,1m2105,1t410101≤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 nn space separated integers p1,p2,,pnp1,p2,…,pn (1pi21051≤pi≤2⋅105 ) — difficulties of the tasks.

    The sum of values nn for all test cases in the input does not exceed 21052⋅105 .

    Output

    Print cc lines, each line should contain answer for the corresponding test case — the maximum possible number of tasks Polycarp can complete and the integer value dd (1dt1≤d≤t ) Polycarp should use in time management rule, separated by space. If there are several possible values dd for a test case, output any of them.

    Examples
    Input
    Copy
    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
    Output
    Copy
    3 5
    4 7
    2 10
    0 25
    Input
    Copy
    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
    Output
    Copy
    4 3
    3 1
    4 5
    Note

    In the first test case of the first example n=5n=5 , m=2m=2 and t=16t=16 . The sequence of difficulties is [5,6,1,4,7][5,6,1,4,7] . If Polycarp chooses d=5d=5 then he will complete 33 tasks. Polycarp will work by the following schedule:

    • Polycarp reads the first task, its difficulty is not greater than dd (p1=5d=5p1=5≤d=5 ) and works for 55 minutes (i.e. the minutes 1,2,,51,2,…,5 );
    • Polycarp reads the second task, its difficulty is greater than dd (p2=6>d=5p2=6>d=5 ) and skips it without spending any time;
    • Polycarp reads the third task, its difficulty is not greater than dd (p3=1d=5p3=1≤d=5 ) and works for 11 minute (i.e. the minute 66 );
    • Polycarp notices that he has finished m=2m=2 tasks and takes a break for 5+1=65+1=6 minutes (i.e. on the minutes 7,8,,127,8,…,12 );
    • Polycarp reads the fourth task, its difficulty is not greater than dd (p4=4d=5p4=4≤d=5 ) and works for 44 minutes (i.e. the minutes 13,14,15,1613,14,15,16 );
    • Polycarp stops work because of t=16t=16 .

    In total in the first test case Polycarp will complete 33 tasks for d=5d=5 . He can't choose other value for dd to increase the number of completed tasks.




    让你求能完成的最大数量 和 对应的d值,d有多个时,输出任意一个
     

    那个任务的难度d不满足二分的性质的,那我们就二分最大的数量,然而这道的难点就是二分了数量之后怎样check,贪心的想,一次只能做m个任务时固定的了,然后时间也是固定的了,然后就是你休息的时间是你完成的任务的难度的综合,当然是选择前mid 的任务啦,那d就选择第mid打的任务难度





     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int MAX=2e5+10;
     4 typedef long long ll;
     5 ll n,m,Time;
     6 ll a[MAX],b[MAX];
     7 
     8 ll cal(ll x,ll d)
     9 {
    10    ll ti=0,now=0;  ll cnt=0; ll sum=0;
    11    for (int i=1;i<=n;i++)
    12    {
    13      if(a[i]>d) continue;
    14      cnt++;  now++;
    15      ti += a[i]; sum += a[i];
    16      if(cnt==x) break;
    17      if(now==m)ti += sum,sum=0,now=0;
    18    }
    19    if(cnt<x)return 0;
    20    return ti<=Time;
    21 
    22 
    23 }
    24 
    25 
    26 int main()
    27 {
    28     int T;
    29     cin>>T;
    30     while(T--)
    31     {
    32         scanf("%lld%lld%lld",&n,&m,&Time);
    33         for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    34         for(int i=1;i<=n;i++)b[i]=a[i];
    35         sort(b+1,b+n+1);
    36         int l=1,r=n,ans=0;
    37         while(r>=l)
    38         {
    39             int mid=(l+r)/2;
    40             if(cal(mid,b[mid]))
    41             {
    42                 l=mid+1;
    43                 ans=mid;
    44             }
    45             else r=mid-1;
    46         }
    47         if(ans==0)puts("0 1");
    48         else printf("%d %lld
    ",ans,b[ans]);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    (笔试题)关于C++的虚函数和多态性
    (笔试题)只用逻辑运算实现乘法运算
    (笔试题)只用逻辑运算实现加法运算
    (C++)浅谈多态基类析构函数声明为虚函数
    (C++)C++多态性中的静态绑定和动态绑定
    (C++)C++类继承中的构造函数和析构函数
    (C++)浅谈using namespace std
    (笔试题)不用除法操作符,实现两个整数的除法
    (笔试题)不用除法操作符,实现两个正整数的除法
    当你学会专注,人生才算真正成熟
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10729055.html
Copyright © 2011-2022 走看看