Pagini recente » Cod sursa (job #2467607) | Cod sursa (job #1597827) | Cod sursa (job #1027152)
#include <iostream>
#include <vector>
#include <algorithm>
#define INF 10000000
using namespace std;
vector<int> st;
class Truck{
public: mutable int capacity, nrT;
Truck(int c):capacity(c),nrT(-1){}
Truck(){}
int getNrT()const{
if(nrT!=-1) return nrT;
else {
nrT = 1;
int s = 0;
for(int i=0;i<st.size();i++) {
if(s==0 && st[i] > capacity)
{ nrT = INF;break;}
else if(s+st[i] > capacity){
s=st[i];nrT++;
}
else s+=st[i];
}
return nrT;
}
}
bool operator<(const Truck& other)const{return this->getNrT()>other.getNrT();}
bool operator>(const Truck& other)const{return this->getNrT()<other.getNrT();}
};
int main(){
int n,k,x;
freopen("transport.in","r",stdin);
freopen("transport.out","w",stdout);
cin>>n>>k;
for(int i=0;i<n;i++) {
cin>>x;
st.push_back(x);
}
vector<Truck> v;
for(int i=0;i<17000;i++) {
v.push_back(Truck(i));
}
Truck mock;
mock.nrT = k;
vector<Truck>::iterator it= lower_bound(v.begin(), v.end(), mock);
cout<<it->capacity;
return 0;
}