hdu 4190(二分)

思路:二分答案每次验证一下是否可行。

代码如下:

 1 /**************************************************
 2  * Author     : xiaohao Z
 3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
 4  * Last modified : 2014-06-21 22:01
 5  * Filename     : hdu_4190.cpp
 6  * Description     : 
 7  * ************************************************/
 8 
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <cmath>
14 #include <algorithm>
15 #include <queue>
16 #include <stack>
17 #include <vector>
18 #include <set>
19 #include <map>
20 #define MP(a, b) make_pair(a, b)
21 #define PB(a) push_back(a)
22 
23 using namespace std;
24 typedef long long ll;
25 typedef pair<int, int> pii;
26 typedef pair<unsigned int,unsigned int> puu;
27 typedef pair<int, double> pid;
28 typedef pair<ll, int> pli;
29 typedef pair<int, ll> pil;
30 
31 const int INF = 0x3f3f3f3f;
32 const double eps = 1E-6;
33 const int LEN = 500000+10;
34 int n, m, num[LEN];
35 
36 bool J(int val){
37     ll ans = 0;
38     for(int i=0; i<n; i++){
39         int tmp = num[i]/val;
40         if(num[i] % val) tmp++;
41         ans += tmp;
42     }
43     if(ans <= m) return true;
44     else return false;
45 }
46 
47 int main()
48 {
49 //    freopen("in.txt", "r", stdin);
50 
51     ios::sync_with_stdio(false);
52     while(cin >> n >> m){
53         if(n == -1 && m == -1) break;
54         int l = 1, r = 1;
55         for(int i=0; i<n; i++){
56             cin >> num[i];
57             r = max(r, num[i]);
58         }
59         while(l < r){
60             int mid = (l + r) / 2;
61             if(J(mid)) r = mid;
62             else l = mid + 1;
63         }
64         cout << l << endl;
65 
66     }
67     return 0;
68 }
View Code

 

posted @ 2014-06-26 10:39  张小豪  阅读(144)  评论(0编辑  收藏  举报