Interviewe(RMQ )

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 n/m, 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?

题意:一共有n名面试者,假设需要录取m名,那么就把原队列分为m段,每段n/m人,后面剩余的就不要了,然后每一组里面选一个最好的,要求选出的m个人总能力要超过k,m最小为多少。

思路:这个肯定会想到需要用到区间最大值,RMQ就很好用了,时间复杂度也还可以接受。在输入的过程中顺便求出所有人的和,和其中最大的值,那么如果和都不到k肯定无法组成,直接输出-1,并且k/max就是最少需要的分组数。如果除出来是0,就改成1,因为有可能一个人的值就已经超过k了。后面就是枚举寻找了,找到第一个合适的分组就直接退出循环就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e5+5;
int dp[maxn][18];
int n;

void st()
{
    for(int j = 1; j <= 18; j++)
        {
            for(int i = 1; i+(1<<(j-1))-1 <= n; i++)
                {
                    dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
                }
        }
}
 
int rmq(int i, int j)
{
    int k = 0;
    while(1<<(k+1) <= j-i+1) k++;
    int ans = max(dp[i][k], dp[j-(1<<k)+1][k]);
    return ans;
}
 
int main()
{
    int k;
    while(~scanf("%d%d", &n, &k))
    {
        int sum = 0, cnt = n, Max = -1;
        if(n < 0 && k < 0)    break;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &dp[i][0]);
            if(dp[i][0] > Max) Max = dp[i][0];
            sum += dp[i][0];
        }
        if(sum <= k)
        {
            printf("-1\n");
            continue;
        }
        st();
        int beg = k/Max;
        if(beg == 0) beg = 1;
        bool jg = false;
        for(int i = beg; i < n; i++)
        {
            sum = 0;
            int t = n/i;
            for(int j = 1; j <= i; j++)
            {
                sum += rmq((j-1)*t+1, j*t);
                if(sum > k)   break;
            }
            if(sum > k)
            {
                cnt = i;
                break;
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值