现在的位置: 首页 > 综合 > 正文

HDOJ 3486 Interviewe RMQ

2018年01月20日 ⁄ 综合 ⁄ 共 2858字 ⁄ 字号 评论关闭

Interviewe

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

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
/*
hdoj 3486 RMQ
具体细节 
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
#define N 200010

int dp[N][20],a[N],n;


void init()
{
    int i,j,limit;
    memset(dp,0,sizeof(dp));
    for(i=1;i<=n;i++)
        dp[i][0]=a[i];
    
    //从第i个开始2^j个数
    for(j=1;j<=log((double)(n+1))/log(2.0);j++)
    {
        limit=n+1-(1<<j);
        for(i=1;i<=limit;i++)
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }    
}

int query(int a,int b)//a,b区间 
{
    int k=(int)(log((double)(b-a+1))/log(2.0));
    return max(dp[a][k],dp[b-(1<<k)+1][k]);
}

int get(int m)//求分成m段情况下,最大能力和 
{
    int i,len,sum=0;
    len=n/m;//每段的长度,一段一段的查找 
    for(i=1;i<=m;i++)
    {
        sum+=query(len*(i-1)+1,len*i);
    }
    return sum; 
} 

int main()
{
    int k,i,j,sum,maxn,minn,left,right,mid,t,ans;
    
    //freopen("test.txt","r",stdin);
    while(scanf("%d%d",&n,&k))
    {
        if(n<0&&k<0) break;
        maxn=-1;minn=99999999;sum=0;
        
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
            if(a[i]>maxn)
                maxn=a[i];
            if(a[i]<minn)
                minn=a[i];
        }
        minn=minn!=0?minn:1;
        maxn=maxn?maxn:1;
        
        if(maxn>=k)
        {//最大的值大于k,只需招一个人,分1段 
            printf("1\n");
            continue;
        }
        if(sum<k)//总和小于k,无法满足 
        {
            printf("-1\n");
            continue;
        }
        
        init();
        
        right=min((k/minn+1),n);
        left=k/maxn;//区间
        
        while(left<=right)//二分枚举 段数 
        {
            mid=(left+right)/2;
            t=get(mid);
            if(t>k)
            {
                right=mid-1;
                ans=mid;
            }
            else
                left=mid+1;
        } 
        printf("%d\n",ans);
    }
    return 0;
} 

抱歉!评论已关闭.