Cod sursa(job #1007975)

Utilizator Athena99Anghel Anca Athena99 Data 9 octombrie 2013 22:35:06
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>

using namespace std;

ifstream fin("transport.in");
ofstream fout("transport.out");

const int nmax= 16000;

int n, k;
int v[nmax+1];

bool check(int x)
{
    int sol= 1, aux= 0;
    for ( int i= 1; i<=n; ++i ) {
        if ( aux+v[i]<=x ) {
            aux+= v[i];
        } else {
            ++sol;
            aux= v[i];
        }
    }

    return sol<=k;
}

int main( )
{
    fin>>n>>k;
    int sol_max= 0, sol_min= 0;
    for ( int i= 1; i<=n; ++i ) {
        fin>>v[i];
        if ( v[i]>sol_min ) {
            sol_min= v[i];
        }
        sol_max+= v[i];
    }

    int step;
    for ( step= 1; step<=sol_max; step*=2 );

    int sol;
    for ( sol= sol_max; step>0; step/=2 ) {
        if ( sol-step>=sol_min && check(sol-step)==1 ) {
            sol-= step; 
        }
    }

    fout<<sol<<"\n";

    return 0;
}