• C. Water the Trees Educational Codeforces Round 126 (Rated for Div. 2)


    There are nn trees in a park, numbered from 11 to nn. The initial height of the ii-th tree is hihi.

    You want to water these trees, so they all grow to the same height.

    The watering process goes as follows. You start watering trees at day 11. During the jj-th day you can:

    • Choose a tree and water it. If the day is odd (e.g. 1,3,5,7,…1,3,5,7,…), then the height of the tree increases by 11. If the day is even (e.g. 2,4,6,8,…2,4,6,8,…), then the height of the tree increases by 22.
    • Or skip a day without watering any tree.

    Note that you can't water more than one tree in a day.

    Your task is to determine the minimum number of days required to water the trees so they grow to the same height.

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases.

    The first line of the test case contains one integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of trees.

    The second line of the test case contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the height of the ii-th tree.

    It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105 (∑n≤3⋅105∑n≤3⋅105).

    Output

    For each test case, print one integer — the minimum number of days required to water the trees, so they grow to the same height.

    Example

    input

    Copy

    3
    3
    1 2 4
    5
    4 4 3 5 5
    7
    2 5 4 8 3 7 4
    

    output

    Copy

    4
    3
    16

    思路:因为我们是奇数天+1,偶数天+2,所以奇数天必须大于等于偶数天,不是随便的天数,那么根据贪心我们应该把所有的天数都变成最大值,然而遇到1 1 1 1 1 1 2这种情况我们最少的天数是把2先变成3然后再把所有都变成3,所以我们判断天数能不能把所有数组都变成最大数或者都变成最大数+1,如果能天数就符合。

    最大最小值用2分来解决,列举天数x,奇数天表示为x-x/2,偶数天表示为x/2,然后我们再看看这些天能不能将所有数组都变成最大值或者最大值加一就行了。

    核心是cheek函数:把奇数天数的个数one,偶数的天数和最大值传入数组,我们遍历数组,如果a[i]<max,那么我们算出他的差值x,先用偶数天数two来填x,如果偶数天数不够的话,把偶数天数用完再用奇数天数,最后看看奇数天数是否大于等于0就行了。

    1. #include<iostream>
    2. #include<algorithm>
    3. #include<cstring>
    4. using namespace std;
    5. int n;
    6. const int N=300005;
    7. typedef long long ll;
    8. ll a[N];
    9. ll b[N];
    10. bool cheek(ll one,ll two,ll max1){
    11. for(int i=1;i<=n;i++){
    12. if(a[i]<max1){
    13. ll x=max1-a[i];
    14. if(x/2<=two){
    15. two-=x/2;
    16. one-=x%2;
    17. }else{
    18. x-=two*2;
    19. two=0;
    20. one-=x;
    21. }
    22. }
    23. }
    24. if(one<0){
    25. return false;
    26. }else return true;
    27. }
    28. void sove(){
    29. cin>>n;
    30. ll max1=0;
    31. for(int i=1;i<=n;i++){
    32. cin>>a[i];
    33. max1=max(max1,a[i]);
    34. }
    35. ll l=0,r=1e18;
    36. while(l<r){
    37. ll mid=l+r>>1;
    38. if(cheek(mid-mid/2,mid/2,max1)||cheek(mid-mid/2,mid/2,max1+1)){
    39. r=mid;
    40. }else l=mid+1;
    41. }
    42. cout<<l<<endl;
    43. }
    44. int main(){
    45. ios::sync_with_stdio(false);
    46. cin.tie() ,cout.tie() ;
    47. int t=1;
    48. cin>>t;
    49. while(t--){
    50. sove();
    51. }
    52. return 0;
    53. }

  • 相关阅读:
    章节六:带参数请求数据
    labview技术交流-判断两个数组的元素是否完全相同
    HTML5期末大作业:用DIV+CSS技术设计的网页与实现(剪纸传统文化网页设计主题)
    python元组与列表的区别
    第2章_瑞萨MCU零基础入门系列教程之面向过程与面向对象
    Zookeeper入门及集群环境搭建
    Debezium报错处理系列之九十九:ConnectException: Source offset ‘file‘ parameter is missing
    数据湖技术之数据中心 Hudi案例实战
    2023-10-08 mysql-代号m-增加外键导致crash-问题分析
    关于网球的要点
  • 原文地址:https://blog.csdn.net/qq_61903556/article/details/125493258