HDU3486 二分+RMQ
来源:互联网 发布:禁止安装任何软件方法 编辑:程序博客网 时间:2024/06/14 15:38
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;}
- hdu3486 RMQ+二分
- HDU3486 二分+RMQ
- HDU3486-RMQ+二分
- HDU3486----Interviewe----二分+一维RMQ
- HDU3486 RMQ
- HDU3486-RMQ
- POJ 2452 RMQ+二分
- hdu 3486 RMQ + 二分
- HDU 3603 二分+RMQ
- ZOJ 3726 RMQ + 二分
- codeforces6E Exposition 二分+RMQ
- RMQ+二分 hdu5289 Assignment
- POJ 2452 (RMQ + 二分)
- hdoj3486Interviewe【RMQ+二分枚举】
- poj 2452(RMQ+二分)
- HDU 3486 RMQ+二分
- hdu 5726 RMQ+二分
- HDU5726 GCD【RMQ+二分】
- 第二次实验
- 蓝桥杯第四届 高斯日记(excel)
- iOS音频格式之AMR和WAV互转(更新支持64位)
- cf463d
- 学生信息管理系统
- HDU3486 二分+RMQ
- 编译运行内核里面的helloworld以及使用递归计算阶乘
- OpenGL函数封装:在任意两个三维点之间绘制给定半径的圆柱体管道
- 增强现实
- 【51NOD 1239】欧拉函数之和
- Java排序算法之快速排序
- webpack loader一览
- cocos2dx-2.2.3下载地址
- 自动化测试入门