Cod sursa(job #1237247)

Utilizator BlueStrutAndrei Prahoveanu BlueStrut Data 3 octombrie 2014 14:07:27
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<cstdio>
using namespace std;
int i, st, dr, n, k, sol, a[16005], mx, mij, sum, x;
bool ok;
int main(){
    freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
    scanf("%d%d", &n, &k); mx=-1;
    for (i=1;i<=n;i++) {scanf("%d", &a[i]); if (a[i]>mx) mx=a[i];} a[n+1]=0;
    st=mx; dr=257000005;
    for (mij=st+(dr-st)/2;st<dr;mij=st+(dr-st)/2) {
        sum=0; x=0; ok=true;
        for (i=1;i<=n;i++) if (sum+a[i]<=mij)  sum+=a[i]; else {
            x++; if (x>k) {ok=false; i=n+1; break;}
            sum=a[i];
        }
        if (sum>0) {x++; if (x>k) ok=false;}
        if (ok==false) st=mij+1; else {sol=mij; dr=mij-1;}
    }
    sum=0; x=0; ok=true;
    for (i=1;i<=n;i++) if (sum+a[i]<=st)  sum+=a[i]; else {
        x++; if (x>k) {ok=false; i=n+1; break;}
        sum=a[i];
    }
    if (sum>0) {x++; if (x>k) ok=false;}
    if (ok==true) sol=st;
    printf("%d\n", sol); return 0;
}