HDU3486 二分+RMQ

YaoYao的公司需要雇佣m名员工,但他没有时间亲自面试。他决定将n名应聘者分成m个组,每个组的能力值之和必须超过目标k。题目要求找到最小的m值,使得应聘者的总能力值大于k,并使用二分搜索来优化解决方案。当无法找到符合条件的m值时,输出-1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这个题首先你要确保n个每个区间只有自己的时候是可以的
然后就想到了暴力
然后明显暴力不行
然后明显就是二分

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int f[200001][30],tu[200001];
int n, m;
void rmq(int c)
{
    int cc = 20;
    for (int a = 1; a <= cc; a++)
    {
        for (int b = 1; b <= c; b++)
        {
            if (b + (1 << a) - 1 <= c)
            {
                f[b][a] = max(f[b][a - 1], f[b + (1 << (a - 1))][a - 1]);
        //      fx[b][a] = min(fx[b][a - 1], fx[b + (1 << (a - 1))][a - 1]);
            }
        }
    }
}
int xunwenda(int zuo, int you)
{
    int k = log2(you - zuo + 1);
    return max(f[zuo][k], f[you - (1 << k) + 1][k]);
}
/*int xunwenxiao(int zuo, int you)
{
    int k = log2(you - zuo + 1);
    return min(fx[zuo][k], fx[you - (1 << k) + 1][k]);
}*/
int jiance(int zhi)
{
    int jiange = n / zhi;
    int jc = 0;
    int b = 1;
    for (int a = 1,b=1; a <= n&&b<=zhi; a += jiange,b++)
    {
        jc += xunwenda(a, a + jiange - 1);
        if (jc > m)return 1;
    }
    if (jc > m)return 1;
    return 0;
}
int main()
{
    while (cin >> n >> m)
    {
        if (n == -1 && m == -1)return 0;
        int he = 0;
        for (int a = 1; a <= n; a++)scanf("%d", &tu[a]);
        for (int a = 1; a <= n; a++)f[a][0] = tu[a],he+=tu[a];
        if (he <= m)
        {
            cout << -1<<endl;
            continue;
        }
        rmq(n);
        int tou = 1, wei = n;
        int jieg = n;
        while (tou <= wei)
        {
            int mid = (tou + wei) / 2;
            if (jiance(mid))
            {
                jieg = mid;
                wei = mid - 1;
            }
            else tou = mid + 1;
        }
        cout << jieg<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值