Pagini recente » Cod sursa (job #2466283) | Cod sursa (job #775995) | Cod sursa (job #2661467) | Cod sursa (job #727482) | Cod sursa (job #1367149)
#include <stdio.h>
int n,nrk,aux,x,li,ls,a[16000],k,flag;
int main()
{
int ax2 = 0;
FILE * f = fopen("transport.in","r");
FILE * g = fopen("transport.out","w");
fscanf(f,"%d%d",&n,&k);
flag =1;
for(int i = 1 ;i<=n;i++)
{
fscanf(f,"%d",&a[i]);
if(a[i]>li) li = a[i];
ls +=a[i];
}
do
{
x = (li+ls)/2;
nrk = 0;
aux = 0;
printf("%d %d\n",li,ls);
for(int i = 1 ;i<=n;i++)
{
aux +=a[i];
if(aux > x)
{
nrk++;
aux = a[i];
}
}
if(nrk >= k)
{
li = x+1;
ax2 = li+1;
}
else {if(nrk < k){ls = x-1;ax2 = ls+1;}}
}while(li<=ls && flag);
fprintf(g,"%d",ax2);
fclose(f);
fclose(g);
return 0;
}