Cod sursa(job #198626)

Utilizator hadesgamesTache Alexandru hadesgames Data 13 iulie 2008 12:01:42
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#include <sstream>
#include <fstream>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define ss second
#define all(c) c.begin(),c.end()
#define pf push_front
#define popb pop_back
#define popf pop_front
stack<int> a;
int k;
bool ver(int x)
{
	stack<int>b(a);
	int s=0,nr=0;
	while (!b.empty())
	{
		if (s+b.top()<=x)
			s+=b.top();
		else
		{
			s=b.top();
			nr++;
		}
		b.pop();
	}
	if (nr>=k)
		return false;
	return true;
	
}
vector<int> c;
int main()
{
	int n,u=0,p,x;
	ifstream in("transport.in");
	ofstream out("transport.out");
	in>>n>>k;
	for(int i=1;i<=n;i++)
	{
		in>>x;
		u+=x;
		c.pb(x);
	}
	for(int i=n-1;i>=0;i--)
		a.push(c[i]);
	p=1;
	while (p<u)
	{
		int m=(p+u)/2;
		if (ver(m))
			u=m;
		else
			p=m+1;
	}
	out<<u;
	in.close();
	out.close();
	return 0;
}