Cod sursa(job #608464)

Utilizator vlad2901Vlad Berindei vlad2901 Data 16 august 2011 19:04:11
Problema Grupuri Scor 32
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>
#define MAX 100000

int a[MAX], n, k;

int bin_search(int, int);
int test(int);

int main()
{
    long long sum = 0;
    int i, m, st, dr, sol, col, lin, ok;

    freopen("grupuri.in", "r", stdin);
    freopen("grupuri.out", "w", stdout);

    scanf("%d %d", &k, &n);

    for(i=0;i<n;++i)
    {
        scanf("%d", &a[i]);
        sum += a[i];
    }

    st = a[n-k];
    dr = sum/k;

    while(st<=dr)
    {
        m = (st + dr)/2;


    col = 0, lin = 0, i = 0;
    ok = -1;

    while(col < k && ok == -1)
    {
        if(i == n)
        {
            ok = 0;
        }

        if(a[i] >= m)
        {
            col += n-i;
            if(col >= k)
            {
                ok = 1;
            }
            ok = 0;
        }
        else
        {
            col = col + (lin + a[i]) / m;
            lin = (lin + a[i]) % m;
        }

        i++;
    }

    ok = 1;


        if(ok)
        {
            st = m+1;
            sol = m;
        }
        else
        {
            dr = m-1;
        }
    }

    printf("%d", sol);

    return 0;
}

int test(int nrg)
{


}