Pagini recente » Cod sursa (job #28963) | Cod sursa (job #1446120) | Cod sursa (job #2225391) | Cod sursa (job #13725) | Cod sursa (job #1057228)
/*#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#define PPD 1000000000
#define PRD 1000000001
using namespace std;
stack<int> st;
vector<int> v;
int ssmax()
{
int sc,smax;
sc=smax=*v.begin();
vector<int>::iterator it;
for(it=v.begin()+1; it<v.end(); ++it)
{
if (sc>=0)
sc+=(*it);
else
sc=(*it);
if (sc>smax)
smax=sc;
}
return smax;
}
int functie()
{
int k=(v.size()-1)/2;
sort(v.begin(),v.end());
return *(v.begin()+k);
}
int main()
{
char c[100005];
int s=0,i,x,sm,nr=0,n=strlen(c);
freopen("expresie1.in","r",stdin);
freopen("expresie1.out","w",stdout);
gets(c);
for(i=0; i<n; ++i)
{
if (c[i]=='(')
{
st.push(PRD);
continue;
}
if (c[i]=='[')
{
st.push(PPD);
continue;
}
if (c[i]==',')
continue;
if (c[i]==')')
{
while(!st.empty() && st.top()!=PRD)
{
v.push_back(st.top());
st.pop();
}
st.pop();
st.push(ssmax());
v.clear();
continue;
}
if (c[i]==']')
{
while(!st.empty() && st.top()!=PPD)
{
v.push_back(st.top());
st.pop();
}
st.pop();
st.push(functie());
continue;
v.clear();
}
sm=1;
x=0;
if (c[i]=='-')
{
sm=-1;
i++;
}
while(i<n && c[i]>='0' && c[i]<='9')
{
x=x*10+(c[i]-'0');
++i;
}
--i;
st.push(sm*x);
++nr;
}
printf("%d\n",nr);
while(!st.empty())
{
s+=st.top();
st.pop();
}
printf("%d\n",s);
return 0;
}
*/
#include <cstdio>
#include <algorithm>
using namespace std;
int f[16005];
int n,k;
inline int f1 (int c)
{
int i,s,t;
s=t=0;
for(i=1; i<=n; ++i)
{
if (f[i]>c)
return k+1;
if (s+f[i]<=c)
s+=f[i];
else
{
s=f[i];
++t;
}
}
if (s)
++t;
return t;
}
inline int binary_search(int n, int val)
{
int st,dr,med,last=0;
st=1;
dr=16000*16005;
while(st<=dr)
{
med=(st+dr)>>1;
if (f1(med)<=k)
{
last=med;
dr=med-1;
}
else
st=med+1;
}
return last;
}
int main ()
{
freopen("transport.in","r",stdin);
freopen("transport.out","w",stdout);
int i;
scanf("%d%d",&n,&k);
for(i=1; i<=n; ++i)
scanf("%d",&f[i]);
printf("%d\n",binary_search(n,k));
return 0;
}