HDU 3486 Interviewe(RMQ+二分)

Mr_Treeeee發表於2020-04-06

Interviewe

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7343    Accepted Submission(s): 1753


Problem Description
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
Hint
We need 3 interviewers to help YaoYao. The first one interviews people from 1 to 3, the second interviews people from 4 to 6, and the third interviews people from 7 to 9. And the people left will be ignored. And the total value you can get is 100+101+100=301>300.
 

Source
 

題意:

給你n個數代表能力,和k。

m代表在每個範圍內選1人,總共選m人,使能力總和超過k,嚴格大於。

求最小的m。

POINT:

假設確定了m,就是一個RMQ問題。詢問m次。

我們可以二分求m。這樣就不會超時了。

注意要嚴格大於,不然超時。TLE了一次。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 200000+5;
int a[N];
int n,k;
int dp[N][30];
int ans=0;
void prermq()
{
    for(int i=1;i<=n;i++)
    {
        dp[i][0]=a[i];
    }
    for(int j=1;1<<j<=n;j++)
    {
        for(int i=1;i-1+(1<<j)<=n;i++)
        {
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
        }
    }
}
int query(int l,int r)
{
    int k=(int)log2((double)(r-l+1));
    return max(dp[l][k],dp[r+1-(1<<k)][k]);
}
int sumk(int m)
{
    int now=0;
    int l=floor(n*1.0/m);
    for(int i=0;i<m;i++)
    {
        now+=query(i*l+1,i*l+l);
    }
    if(now>k)
    {
        ans=min(ans,m);
        return 1;
    }
    else return 0;
}
void solve()
{
    int l=1,r=n;
    int mid=(l+r)>>1;
    while(mid!=r)
    {
        if(sumk(mid)==1)
            r=mid;
        else
            l=mid+1;
        mid=(l+r)>>1;
    }
    sumk(mid);
    sumk(l);
    sumk(r);
}
int main()
{
    int sum;
    while(~scanf("%d %d",&n,&k))
    {
        sum=0;
        if(n==-1) break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum<=k)
        {
            printf("-1\n");
            continue;
        }
        prermq();
        ans=N;
        solve();
        if(ans==N) printf("-1\n");
        else printf("%d\n",ans);
    }
    
    
}