2014
04-04

# 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 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?

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
HintWe 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. 

#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;
}

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