Cod sursa(job #3132920)

Utilizator Dragos__1_1Dragos Antohi Dragos__1_1 Data 24 mai 2023 13:34:58
Problema Transport Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
int k,nr,v[16001],s;
int poz,st,dr,mid,i;
int sectiuni(int x,int m)
{   if(x<m)return-1;
    int suma=0,i=1,cnt=1;
    while(i<=nr)
    {   if (suma+v[i]<=x)suma+=v[i];
        else {suma=v[i];cnt++;}
        i++;
    }
    return cnt;
}
int main()
{   f>>nr>>k;
    int maxi=0;
    for(i=1;i<=nr;i++){f>>v[i];if (v[i]>maxi)maxi=v[i];s+=v[i];}

    st=1;
    dr=s;
    while(st<=dr)
    {
        mid=(st+dr)/2;
        int x=sectiuni(mid,maxi);
        if(x==-1)st=mid+1;
        else if(x<k){dr=mid-1;}
        else if (x>=k){st=mid+1;}

    }
    g<<mid<<'\n';
    return 0;
}