Divide and conquer:Drying(POJ 3104)

                 

                 烘干衣服

  题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服?

  简单来说题目就是要:时间允许的情况下让时间最小,时间可以无限大,这题就是“最小化最大值”,二分法

  

 1 #include 
 2 #include 
 3 #include 
 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))