zoukankan      html  css  js  c++  java
  • HDU 4190 Distributing Ballot Boxes【二分答案】

    题意:给出n个城市,n个城市分别的居民,m个盒子,为了让每个人都投上票,问每个盒子应该装多少张票

    二分盒子装的票数,

    如果mid<=m,说明偏大了,r应该向下逼近 ,r=mid

    如果mid>m,说明偏小了,l应该向上逼近,l=mid+1

    和上午那个切割木块那个一样-----------

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 typedef long long LL;
    14 const int INF = (1<<30)-1;
    15 const int mod=1000000007;
    16 const int maxn=1000005;
    17 
    18 int a[maxn];
    19 int n,m;
    20 
    21 int ok(int x){
    22     int ans=0;
    23     for(int i=1;i<=n;i++){
    24         int tmp=a[i]/x;
    25         if(a[i]%x) tmp++;
    26         ans+=tmp;
    27     }
    28     if(ans<=m) return 1;
    29     return 0;
    30 }
    31 
    32 int main(){
    33     while(scanf("%d %d",&n,&m)!=EOF){
    34         if(n==-1&&m==-1) break;
    35         
    36         int maxx=-INF;
    37         for(int i=1;i<=n;i++) {
    38             scanf("%d",&a[i]);
    39             maxx=max( a[i] , maxx );
    40         }
    41         
    42         int l=0,r=maxx,mid;
    43         while(l<r){
    44             mid=(l+r)/2;    
    45             if(ok(mid)) r=mid;
    46             else l=mid+1;
    47         }
    48         printf("%d
    ",l);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    周总结9
    TDtree冲刺第十天
    规划极限编程阅读笔记03
    TDtree冲刺第九天
    TDtree第八天
    规划极限编程阅读笔记02
    TDtree冲刺第七天
    周总结8
    TDtree冲刺第六天
    11/1
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4542673.html
Copyright © 2011-2022 走看看