首页 > ACM题库 > HDU-杭电 > hdu 3846-pyramids-动态规划-[解题报告]hoj
2015
04-13

hdu 3846-pyramids-动态规划-[解题报告]hoj

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.

这个题目是rmq先打出st表,然后暴搜,首先找到整个区间的最大值max,那么他需要的人数最少是(k / max );所以从这个点开始暴搜,一般的暴搜会超时;注意这里的(k / max )可能为0 ,那么就应该分类讨论,注意这里不能用二分查找因为这个函数不是单调的,如果最后一个人的值很大,然后 ans = n;的时候选上了可能满足,但是如果n+1的时候选不上,那么就可能不满足,所以这个函数不是单调的;

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int sum,k;
int num[2000005];
int dp[2000005][40];
void rmp_st(int n)
{
    int i,j;
    for(i=0;i<n;i++)
       dp[i][0]=num[i];
    for(j=1;j<=(int)(log((double)n)/log(2.0));j++)
    {
        for(i=0;i+(1<<j)-1<n;i++)
           dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }
}
int rmp_find(int L,int R)
{
    int x=(int)(log(double(R-L+1))/log(2.0));
    return max(dp[L][x],dp[R-(1<<x)+1][x]);
}

bool devide( int n )
{
    int people = sum / n;
    int marks = 0;
    for ( int i = 0; i < n; i++)
    {
        marks += rmp_find(i*people,i*people+people-1);
        if ( marks > k ) return true;
    }
    return false;
}
int main()
{

    while ( scanf("%d %d",&sum,&k))
    {
        if ( sum == -1 ) break;
        int maxsum = 0,flag = 0;
        for ( int i = 0; i < sum; i++)
        {
            scanf("%d",&num[i]);
            if (!flag) maxsum += num[i];
            if ( maxsum >= k ) flag = 1;
        }
        if ( !flag )///如果flag == 1 就是说明所有人加起来也不能凑足k
        {
            printf("-1\n"); continue;
        }
        rmp_st(sum);
        int t = rmp_find(0,sum-1);
        if ( t > k )///题目要求严格大于;
        {
            printf("1\n"); continue;
        }
        int ans = k/t;///这里的ans可能为0;所以要在上面分情况讨论
        for(;;)
        {
            if(!devide(ans)) ans++;
            else break;
        }
        printf("%d\n",ans);
    }

}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。