Cod sursa(job #864185)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 24 ianuarie 2013 18:50:33
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#define nmax (1<<14)
using namespace std;

int N,K,Sol,Min,Max,A[nmax];

bool Check(int Val) {

    int i,Sum,k;

    if(Val<Min)
        return 0;

    for(i=1,Sum=0,k=1;i<=N;i++) {

        if(Sum+A[i]>Val) {
            Sum=A[i];
            k++;
            }
        else
            Sum+=A[i];

        }

    return (k<=K);

}
int BinarySearch() {

    int i,step;

    for(step=1;step<Max;step<<=1);

    for(i=0;step;step>>=1)
        if(i+step<=Max && !Check(i+step))
            i+=step;

    return i+1;

}
void solve() {

    for(int i=1;i<=N;i++)
        Max+=A[i],
        Min=(A[i]>Min?A[i]:Min);

    Sol=BinarySearch();

}
void read() {

    ifstream in("transport.in");

    in>>N>>K;
    for(int i=1;i<=N;i++)
        in>>A[i];

    in.close();

}
void write() {

    ofstream out("transport.out");
    out<<Sol<<'\n';
    out.close();

}
int main() {

    read();
    solve();
    write();

    return 0;

}