考点:
离线RMQ,二分法
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 $\lfloor \frac{n}{m} \rfloor$ , 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.
参考文献
算法训练营
点拨
本题在建立好ST表的基础上,进行二分测试。
三点注意:
1. k值比较大,使用 long long 保险。
2. 二分严格遵循模板,方可一遍过。
3.judge函数最后return sum >k
不可以换成>=,因为judge只是测试mid是否在可行区间内,而不是可行区间端点,我们的二分找的是区间端点,不可越殂代疱。
C++ 代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std ;
typedef long long LL ;
const int N = 200010 ;
LL n , k ;
int F[N][25] ;
int a[N] ;
void create_st()
{
for(int i = 1 ; i <=n ; i ++)
{
F[i][0] = a[i] ;
}
int k = log2(n) ;
for(int j = 1 ; j <= k ; j ++)
{
for(int i = 1; i <= n-(1<<j) +1 ; i ++)
{
F[i][j] = max(F[i][j-1] , F[i+(1<<(j-1))][j-1]) ;
}
}
}
LL rmq(int l , int r)
{
int k = log2(r-l+1) ;
return max(F[l][k] , F[r-(1<<k) +1 ][k]) ;
}
bool judge(int mid)
{
LL sum = 0 ;
int t = n/mid ;
for(int i = 0 ; i < mid ; i ++ )
{
sum += rmq(1 + i *1ll* t , t+ i *1ll* t ) ;
}
return sum > k ;
}
int main()
{
while(scanf("%lld%lld" ,&n,&k))
{
if(n == -1 && k == -1) break;
memset(F,0,sizeof F) ;
memset(a,0,sizeof a) ;
LL tmp = 0 ;
for(int i = 1 ; i<=n ; i ++)
{
scanf("%d",&a[i]) ;
tmp += a[i] ;
}
if(tmp < k )
{
puts("-1") ;
continue ;
}
create_st() ;
int l = 1 , r = n ;
while(l < r)
{
int mid = l + r >> 1 ;
if(judge(mid)) r = mid ;
else l = mid +1 ;
}
printf("%d\n" , l) ;
}
return 0 ;
}