Cod sursa(job #2282721)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 14 noiembrie 2018 13:54:48
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
int maxx,st,dr,mij,v[1000009],n,k,ctr,cmij,s;
int main()
{
    f >> n>>k;
    for (int i=1;i<=n;i++) {
        f >> v[i];
        s+=v[i];
        if (v[i]>maxx)
            maxx=v[i];
    }
    st=maxx;
    dr=s;
    mij=(st+dr)/2;
    while (st!=mij && dr!=mij) {
            cmij=mij;
            ctr=0;
            for (int j=1;j<=n;j++){
                    if (v[j]<cmij)
                        cmij-=v[j];
                    else {
                        ctr++;
                        cmij=mij;
                        cmij-=v[j];
                    }

            }
        if (ctr<k)
            dr=mij;
        else
            st=mij;
        mij=(st+dr)/2;

    }
    g<< mij;



    return 0;
}