Friday, 21 September 2012

UVa 12390 - Distributing Ballot Boxes Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<63)-1
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define MOD 1000000007
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define KS printf("Case %d: ",kk++)
#define NL printf("\n")
#define MX 5000010


int n,m,arr[MX];

bool chk(int md)
{
 int tmp, mm=m;
 for (int i = 0; i < n; i++)
 {
  if (arr[i] % md == 0)
   tmp = arr[i] / md;
  else
   tmp = arr[i] / md + 1;
  mm -= tmp;
  if (mm < 0)
   return false;
 }
 return true;
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        if (n==-1 && m==-1) break;
        int mx=0;
        for (int i = 0; i < n; i++)
   {
       scanf ("%d", &arr[i]);
       mx=max(mx,arr[i]);
   }

        int lft=0,rgt=mx,mid;
        while(lft<rgt)
        {
            mid=(lft+rgt)/2;
            if(chk(mid))
                rgt=mid;
            else
                lft=mid+1;

        }
        cout<<lft<<endl;
    }
return 0;
}

No comments:

Post a Comment