题意:给你每个理发师的理发时间,问你排在队列中的第N个位置,问你应该被哪个理发师剪发。
解题思路:二分时间,看这个时间到第几个人理发了,然后找到临界值,看这个值的时候有那些理发师接待了新旅客的,即可找到那个理发师。
解题代码:
1 // File Name: b.sample.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月18日 星期六 09时05分30秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 26 using namespace std; 27 LL a[1005]; 28 LL b , n ; 29 LL count(LL t){ 30 LL sum = 0 ; 31 for(int i = 1;i <= b;i ++) 32 { 33 sum += (t/a[i]) + 1 ; 34 } 35 return sum; 36 } 37 LL fen(LL l , LL r){ 38 LL m = (l + r)/2; 39 while(l<= r){ 40 //printf("***\n"); 41 m = (l + r)/2; 42 LL t = count(m); 43 if(t >= n){ 44 r = m - 1; 45 }else l = m + 1; 46 } 47 return l ; 48 } 49 int main(){ 50 // freopen("B-large.in","r",stdin); 51 // freopen("output","w",stdout); 52 LL T; 53 scanf("%lld",&T); 54 for(int ca = 1; ca <= T ;ca ++){ 55 scanf("%lld %lld",&b,&n); 56 for(LL i = 1;i <= b;i ++) 57 scanf("%lld",&a[i]); 58 printf("Case #%d:",ca); 59 if(n <= b ) 60 { 61 printf(" %lld\n",n); 62 continue; 63 } 64 LL t = fen(1,1e15); 65 LL num = count(t-1); 66 for(int i = 1;i <= b;i ++){ 67 if(t% a[i] == 0){ 68 num ++; 69 } 70 if(num == n) 71 { 72 printf(" %d\n",i); 73 break; 74 } 75 } 76 77 } 78 79 return 0; 80 }