HDU6383p1m2(二分)

补个题。。

传送门 点我

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1858    Accepted Submission(s): 676

度度熊很喜欢数组!!

我们称一个整数数组为稳定的,若且唯若其同时符合以下两个条件:

Input
N>30000
 

 

 

Output
对于每一组测试数据,请依序各自在一行内输出一个整数,代表可能到达的平衡状态中最大的『数组中的最小值』,如果无法达成平衡状态,则输出-1. -1-1123
Sample Input
2
3
1 2 4
2
0 100000000
 
Sample Output
2
33333333
 
Source

 

思路:
最大的『数组中的最小值』指的是所有经过操作满足题意的序列中,每个序列里面的最小值提出来,取最大值。
比如说[-1,2,4]和[2,2,1]前者最小值是-1,后者最小值是2,取这俩的最大值,即2。

题目中最大的『数组中的最小值』,可以对这个值进行二分,遍历整个输入的数组
判断通过操作之后,可不可以到达这个当前的“最大的『数组中的最小值』”。
如果可以,则提高左界限L,如果不可以则降低右界限R,直到满足条件。

代码:

#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
LL a[300010];
int main()
{ 
     int _;
    for(scanf("%d",&_);_--;){
        int n;
        scanf("%d",&n);
        for(int i = 0 ; i < n ; i ++){
            scanf("%lld",a+i);
        }
        LL l = 1 ,r = 100000000;
        while( l <= r){
            LL mid = (l+r)>>1;
            LL temp = 0;
            for(int i = 0 ; i < n ; i ++){
                if(a[i] > mid){
                    temp += (a[i]-mid)/2;
                }else{
                    temp += a[i]-mid;
                }
            }
            if(temp >= 0){
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }
        printf("%lld\n",r);
    } 
}
/*
2
3
1 2 4
2
0 100000000

2
33333333
 
*/ 

 

posted on 2019-08-08 15:40  Esquecer  阅读(83)  评论(0编辑  收藏  举报

导航