Divide and conquer:Drying(POJ 3104)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Divide and conquer:Drying(POJ 3104)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
烘干衣服
題目大意:主人公有一個烘干機,但是一次只能烘干一件衣服,每分鐘失水k個單位的水量,自然烘干每分鐘失水1個單位的水量(在烘干機不算自然烘干的那一個單位的水量),問你最少需要多長時間烘干衣服?
簡單來說題目就是要:時間允許的情況下讓時間最小,時間可以無限大,這題就是“最小化最大值”,二分法
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 5 using namespace std; 6 7 static int clothes[100005]; 8 9 void Search(const int, const int); 10 bool judge(const long long, const int, const int); 11 int fcmop(const void *a, const void *b) 12 { 13 return *(int *)a - *(int *)b; 14 } 15 16 int main(void)//在時間允許的情況下讓值最小(最小化最大值) 17 { 18 int sum_clothes, re; 19 20 while (~scanf("%d", &sum_clothes)) 21 { 22 for (int i = 0; i < sum_clothes; i++) 23 scanf("%d", &clothes[i]); 24 scanf("%d", &re); 25 qsort(clothes, sum_clothes, sizeof(int), fcmop); 26 if (re == 1) 27 printf("%d\n", clothes[sum_clothes - 1]);//注意一定不要出現0的情況 28 else 29 Search(sum_clothes, re); 30 } 31 return 0; 32 } 33 34 void Search(const int sum_clothes, const int re) 35 { 36 long long lb = 0, rb = (long long)10e+9 + 1, mid; 37 38 while (rb - lb > 1) 39 { 40 mid = (rb + lb) / 2; 41 if (judge(mid, sum_clothes, re)) rb = mid; 42 else lb = mid; 43 } 44 printf("%lld\n", rb); 45 } 46 47 bool judge(const long long times, const int sum_clothes, const int re) 48 { 49 long long use_time_now, use_sum = 0; 50 int i; 51 52 for (i = 0; i < sum_clothes && clothes[i] <= times; i++); 53 54 for (; i < sum_clothes; i++) 55 { 56 use_time_now = (clothes[i] - times + re - 1 - 1) / (re - 1);//向上取整,要先-1 57 use_sum += use_time_now; 58 if (use_sum > times) 59 return false; 60 } 61 return true; 62 }
?
轉載于:https://www.cnblogs.com/Philip-Tell-Truth/p/5130951.html
總結
以上是生活随笔為你收集整理的Divide and conquer:Drying(POJ 3104)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: uiautomator的坑和AAPT命令
 - 下一篇: 你在乎的--世界在乎的