您的位置:首页 > 大数据 > 人工智能

Google Code Jam Round 1A 2015 Problem B. Haircut

2015-04-18 19:02 2176 查看

Problem

You are waiting in a long line to get a haircut at a trendy barber shop. The shop has Bbarbers on duty, and they are numbered 1 through B. It always takes the kth
barber exactly Mk minutes to cut a customer's hair, and a barber can only cut one customer's hair at a time. Once a barber finishes cutting hair, he is immediately free to help another customer.

While the shop is open, the customer at the head of the queue always goes to the lowest-numbered barber who is available. When no barber is available, that customer waits until at least one becomes available.

You are the Nth person in line, and the shop has just opened. Which barber will cut your hair?

Input

The first line of the input gives the number of test cases, T. T test cases follow; each consists of two lines. The first contains two space-separated integers B and N --
the number of barbers and your place in line. The customer at the head of the line is number 1, the next one is number 2, and so on. The second line contains M1, M2, ..., MB.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the number of the barber who will cut your hair.

Limits

1 ≤ T ≤ 100.

1 ≤ N ≤ 109.

Small dataset

1 ≤ B ≤ 5.

1 ≤ Mk ≤ 25.

Large dataset

1 ≤ B ≤ 1000.

1 ≤ Mk ≤ 100000.

Sample

Input

Output

3
2 4
10 5
3 12
7 7 7
3 8
4 2 1

Case #1: 1
Case #2: 3
Case #3: 1

In Case #1, you are the fourth person in line, and barbers 1 and 2 take 10 and 5 minutes, respectively, to cut hair. When the shop opens, the first customer immediately has the choice of barbers 1 and
2, and she will choose the lowest-numbered barber, 1. The second customer will immediately be served by barber 2. The third customer will wait since there are no more free barbers. After 5 minutes, barber 2 will finish cutting the second customer's hair, and
will serve the third customer. After 10 minutes, both barbers 1 and 2 will finish; you are next in line, and you will have the choice of barbers 1 and 2, and will choose 1.
解析
很简单啦,就是二分等待时间。画张表手推一下就好了。
赠送数据一组:

3 5
7 8 7
答案是 3
#include<cstdio>

using namespace std;

long long N,B,M[1010];
bool count(long long x)
{
long long sum=0;
for(int i=1;i<=B;i++) sum+=x/M[i]+1;
return sum>=N;
}
int work()
{
long long l=0,r=N*M[1];//[l,r)
while(l<r)
{
long long mid=l+(r-l)/2;
if(count(mid)) r=mid;
else l=mid+1;
};

long long lastnum=0,cnt=0;
for(int i=1;i<=B;i++) lastnum+=(l-1)/M[i]+1;
//printf("time %lld lastnum %lld %lld\n",l,lastnum,N-lastnum);
//if(N==lastnum) return B;
for(int i=1;i<=B;i++)
if(l % M[i]==0) if(cnt+1==N-lastnum) return i; else cnt++;
}
int main()
{
//freopen("B.in","r",stdin);
//freopen("B.out","w",stdout);
int T; scanf("%d",&T);
for(int i=1;i<=T;i++)
{
scanf("%lld%lld",&B,&N);
for(int j=1;j<=B;j++) scanf("%lld",&M[j]);
printf("Case #%d: %d\n",i,work());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: