Pagini recente » Cod sursa (job #368303) | Cod sursa (job #226419) | Cod sursa (job #32065) | Cod sursa (job #2757141) | Cod sursa (job #894397)
Cod sursa(job #894397)
#include<fstream>
using namespace std;
int v[16010], n , k, camion_max, camion_min, m;
ifstream fin("transport.in");
ofstream fout("transport.out");
inline void citire()
{
fin >> n >> k;
for(int i = 1; i <= n; ++i)
{
fin >> v[i];
if(v[i] > camion_min) camion_min = v[i];
camion_max += v[i];
}
}
int main()
{
citire();
long long sol = 800000;
int suma = 0, contor = 0;
int q;
m = (camion_min + camion_max)/2;
while(camion_min <= camion_max)
{
for(q = 1; q<= n; ++q)
{
if(suma + v[q] <= m)
{
suma += v[q];
}
else
{
contor++;
suma = v[q];
}
}
if(suma <= m)++contor;
if(/*sol > m and*/ contor <= k) sol = m;
if(contor <= k)
{
camion_max = m - 1;
m = (camion_min + camion_max)/2;
}
if(contor > k)
{
camion_min = m + 1;
m = (camion_min + camion_max)/2;
}
contor = 0;
suma = 0;
}
fout << sol;
return 0;
}
//#include <iostream>
//#include <fstream>
//using namespace std;
//
//ifstream fin("transport.in");
//ofstream fout("transport.out");
//
//int hi, lo = 1, mid;
//int i, n, t, tmax, s, v[16005];
//
//void go() {
// t = 0;
// i = 1;
// while (i <= n && t <= tmax) {
// s = 0;
// while (s < mid && i <= n) {
// s += v[i];
// ++i;
// }
// if (s > mid)
// --i;
// ++t;
// }
//}
//
//int binarySearch() {
// hi = 16001 * 16000;
// while (lo <= hi) {
// mid = lo + (hi - lo) / 2;
// go();
// if (t <= tmax)
// hi = mid - 1;
// else
// lo = mid + 1;
// }
// if (t > tmax)
// return mid + 1;
// return mid;
//}
//
//int main() {
// fin >> n >> tmax;
// for (i = 1; i <= n; ++i)
// fin >> v[i];
// fout << binarySearch();
// return 0;
//}