【RMQ的应用】Interviewe

Interviewe

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


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.
 
题目大意:

   一个公司要招收人员,然而有很多人员。公司每次招收人员有自己的规则,每次招收只要招收的人员的能力值总和达到K就不再招收人员。

  由于公司的老板很忙,他会根据面试人的访问顺序排成队列,然后把N个人的队列按照M个人一组分为一段,每次从按队列的顺序取M个人,(后面不足M个人的面试人忽略掉,当作这些面试人态度不端正不合格),既把N个人分成N/M段,在每一段中的M个人中取能力值最大的那一个。如果,所取的人员的能力值总和大于K就直接取走这几个人。由于雇佣每个人的费用都是一样的,为了使得公司话费最少,求出满足能力值总和的招收的最少人员个数。如果都不能满足题意的话,则输出-1。(去N/M个人)

  解法:枚举每一个区间长度,用RMQ获取区间的最大值,每次记录最少的人员个数,直到,枚举所有的区间长度。输出答案即可。

  还需要注意的一点是,枚举区间的长度还需要优化,不然每次都枚举区间长度1~200000,再去每次取区间最值,肯定会超时。所以可以根据这个区间长度进行优化,在你输入的能力值中去最大值Max,然后把K/Max,这个数值就是每次枚举最小区间长度。

代码:2015.8.12

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #define max(a,b) (a)>(b)?(a):(b)
 5 #define min(a,b) (a)<(b)?(a):(b)
 6 #define MAX 200010
 7 using namespace std;
 8 int maxsum[MAX][20];
 9 int Num[MAX];
10 void Cread_RMQ(int N)
11 {
12     for(int j = 1; j <20; j++)
13         for(int i = 1; i <= N; i++){
14             int TMD=i+(1<<(j-1));
15             if(i+(1<<j)-1<= N)
16             {
17                 maxsum[i][j]=max(maxsum[i][j-1],maxsum[TMD][j-1]);
18            }
19         }
20 }
21 int RMQ(int l,int r)
22 {
23     int k,TMD,Max;
24     k=(int)(log(r-l+1.0)/log(2.0));
25     TMD=r-(1<<k)+1;
26     Max=max(maxsum[l][k],maxsum[TMD][k]);
27    return Max;
28 }
29 int main()
30 {
31     int N,i,j,K,a,b,k,sign,Max,Count,l;
32     while( scanf("%d%d",&N,&K)!=EOF)
33     {
34         if(N<0&&K<0)break;
35         for(i=1,Count=0,Max=-1;i<=N;i++)
36         {
37             scanf("%d",&Num[i]);
38             maxsum[i][0]=Num[i];
39             Count+=Num[i];
40             if(Num[i]>Max)Max=Num[i];
41         }
42         if(Count<=K){printf("-1\n");continue;}
43         Cread_RMQ(N);
44         sign=0;
45         Max=K/Max;//优化
46        if(Max==0)j=1;
47         else j=Max;
48         int ans=N;
49         while(j<=N)
50         {
51 
52             int Sum=0;
53             k=N/j;
54             for(i=1,l=1;i+k-1<=N;i+=k,l++)
55             {
56                 Sum+=RMQ(i,i+k-1);
57                 if(Sum>K)
58                 {
59                     sign=1;
60                     if(ans>l)ans=l;
61                 }
62             }
63             if(sign)break;
64             j++;
65         }
66         if(sign)printf("%d\n",ans);
67         else printf("-1\n");
68     }
69     return 0;
70 }
View Code

 

posted @ 2015-08-13 00:26  Wurq  阅读(501)  评论(0编辑  收藏  举报