Interviewe

来源:互联网 发布:怎么在电脑上清理数据 编辑:程序博客网 时间:2024/05/22 04:40

Interviewe

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 734    Accepted Submission(s): 183

 
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, 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 3007 100 7 101 100 100 9 100 100 110 110-1 -1
 
RMQ。列举面试人员数目。
 
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 200000 + 10;const int MAXJ = 22;int a[MAXN], d[MAXN][MAXJ];int n, k;int RMQ_init(){for(int i = 0; i < n; i++) d[i][0] = a[i];for(int j = 1; (1 << j) <= n; j++){for(int i = 0; i + (1 << j) - 1 <= n; i++){d[i][j] = max(d[i][j-1], d[i+(1 << (j-1))][j-1]); }}}int RMQ(int L, int R){int k = 0; while((1 << (k+1)) <= R - L + 1) k++;return max(d[L][k], d[R - (1 << k) + 1][k]);}int main(){while(scanf("%d%d", &n, &k) && (n > 0 || k > 0)){memset(d, 0, sizeof(d));int maxi = 0, s = 0;for(int i = 0; i < n; i++){scanf("%d", &a[i]);maxi = max(a[i], maxi);s += a[i];}RMQ_init();int m;if(s <= k){m = n+1;}else{m = k / maxi;if(!m) m++; //m的最小值 int seg = 0;  // 每一个人面试的人数 for(; m <= n; m++){int res = 0, i = 0;seg = n / m;int j = m;while(j--){res += RMQ(i, i + seg - 1);i += seg;}if(res > k) break;}}if(m > n) printf("-1\n");else printf("%d\n", m);}return 1;}


 

0 0
原创粉丝点击