首页 > ACM题库 > HDU-杭电 > HDU 3486-Interviewe-线段树-[解题报告]HOJ
2014
04-04

HDU 3486-Interviewe-线段树-[解题报告]HOJ

Interviewe

问题描述 :

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 isCount 101 , 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?

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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.

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3486

题目大意:给出n(1<n<200000)个数,将其分为m段,每段长度为n/m,每段中选出最大的数,使得选出数的和大于k,问最小的m是多少。

解题思路:据说有人用二分过了,这只能说数据太水,明显不满足二分的性质。

本题只能采用枚举,枚举答案,这里很容易把枚举写成(n^2)的,可以枚举每段长度,在求最小的个数,如果是枚举段数的话,要注意对与两种用长度一样的情况可以利用上一种的计算结果,这样枚举复杂度是(nlogn)的。

本题要求出一段的最大值,很多采用线段树,这样就会TLE,对于不用修改的,明显RMQ更优,查询时O(1)的,这样就可以通过了。

通过代码:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>


#define N 200020
using namespace std;
int RMQ[N][20];
int log[N];
int query(int l,int r){
    int lg=log[r-l+1];
    return max(RMQ[l][lg],RMQ[r-(1<<lg)][lg]);
}
int main(){
    for (int i=0;i<18;i++)
        for (int j=1<<i;j<(1<<(i+1))&&j<N;j++)
            log[j]=i;
    int n,k;
    while (scanf("%d%d",&n,&k)==2){
        if (n<0&&k<0) break;
        for (int i=1;i<=n;i++)
            scanf("%d",&RMQ[i][0]);
        for (int j=1;j<18;j++)
            for (int i=1;i<=n;i++){
                if (i+(1<<j)>n) break;
                RMQ[i][j]=max(RMQ[i][j-1],RMQ[i+(1<<(j-1))][j-1]);
            }
        int ans=-1;
        int sum;
        for (int i=1;i<=n;i++){
            int seg=n/i;
            int l,j;
            if (i>1&&seg==n/(i-1)){
                l=seg*(i-1)+1;
                j=i;
            }
            else {
                l=1;
                sum=0;
                j=1;
            }
            for (;j<=i;j++){
                sum=sum+query(l,l+seg-1);
                l=l+seg;
            }
            if (sum>k) {ans=i;break;}
        }
        printf("%d\n",ans);
    }
    return 0;
}

参考:http://blog.csdn.net/ruptins/article/details/21417775


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮