L - Interviewe HDU - 3486(RMQ+二分)

YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there are n people coming for the interview. However, YaoYao is so busy that he has no time to interview them by himself. So he decides to select exact m interviewers for this task. 
YaoYao decides to make the interview as follows. First he queues the interviewees according to their coming order. Then he cuts the queue into m segments. The length of each segment is  , which means he ignores the rest interviewees (poor guys because they comes late). Then, each segment is assigned to an interviewer and the interviewer chooses the best one from them as the employee. 
YaoYao’s idea seems to be wonderful, but he meets another problem. He values the ability of the ith arrived interviewee as a number from 0 to 1000. Of course, the better one is, the higher ability value one has. He wants his employees good enough, so the sum of the ability values of his employees must exceed his target k (exceed means strictly large than). On the other hand, he wants to employ as less people as possible because of the high salary nowadays. Could you help him to find the smallest m? 

Input

The input consists of multiple cases. 
In the first line of each case, there are two numbers n and k, indicating the number of the original people and the sum of the ability values of employees YaoYao wants to hire (n≤200000, k≤1000000000). In the second line, there are n numbers v1, v2, …, vn (each number is between 0 and 1000), indicating the ability value of each arrived interviewee respectively. 
The input ends up with two negative numbers, which should not be processed as a case. 

Output

For each test case, print only one number indicating the smallest m you can find. If you can’t find any, output -1 instead.

Sample Input

11 300
7 100 7 101 100 100 9 100 100 110 110
-1 -1

Sample Output

3

题意:n个人排队面试,每个人能力使A[i],老板要把他们分成m段,不能整除的,剩余的淘汰,然后从m段中选取最大值累加得到的值是否大于老板的期望值。

思路:因为要查询某个区间最大值不用更新用RMQ算法查比用线段树要快,

/*
  RMQ + 二分
*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#define ll long long
#define ull unsigned long long
#define inf 0x3f3f3f3f
const int mod=1000000007;
const int N=200005;
using namespace std;
int a[N][20];int n,m;
int A[N];int sum;
void st_init()
{
    sum=0;
    for(int i=1;i<=n;i++)
    {
        a[i][0]=A[i];
        sum+=A[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        int d=(1<<j);
        for(int i=1;i+d-1<=n;i++)
        {
            a[i][j]=max(a[i][j-1],a[i+d/2][j-1]);
        }
    }
}
int get_max(int l,int r)
{
    int k=(int)(log(r-l+1.0)/log(2.0));
    return 
        max(a[l][k],a[r-(1<<k)+1][k]);
}
bool tao(int mid)
{
    int d=n/mid;
    int ans=0;
    int mm=d*mid;
    for(int i=1;i<=mm;i+=d)
    {
        ans+=get_max(i,i+d-1);
    }
    return ans>m;
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        if(n==-1||m==-1)
            return 0;
        for(int i=1;i<=n;i++)
            scanf("%d",&A[i]);
        st_init();
        if(sum<=m)
        {
            printf("-1\n");
            continue;
        }
        int l=1,r=n;
        while(r-l>0)
        {
            int mid=(l+r)>>1;
            if(tao(mid))
                r=mid;
            else
                l=mid+1;
        }
        printf("%d\n",r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Kuguotao/article/details/89374432