Cod sursa(job #1053199)
Utilizator | Data | 12 decembrie 2013 15:14:57 | |
---|---|---|---|
Problema | Transport | Scor | 50 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 2.46 kb |
#include <fstream>
using namespace std;
int main()
{
ifstream fin("transport.in");
ofstream fout("transport.out");
int n,k,last=-1,c=-1,temp=0,temp2,temp3=0;
fin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
fin>>a[i];
temp+=a[i];
}
temp/=k;
while(1)
{
temp2=temp;
temp3=1;
for(int i=0;i<n;i++)
{
if(temp2-a[i]>=0)
{
temp2-=a[i];
}
else
{
if(temp2==temp)
{
last=c;
c=0;
temp++;
break;
}
else
{
temp2=temp;
temp3++;
if(temp2-a[i]>=0)
{
temp2-=a[i];
}
else
{
last=c;
c=0;
temp++;
break;
}
}
}
if(i==n-1)
{
if(temp3<=k)
{
last=c;
c=1;
}
else
{
last=c;
c=0;
}
}
}
if(c==1&&temp3<=k)
{
temp--;
}
else if(c==0)
{
temp++;
}
else if(temp3>k)
{
temp++;
}
if(c==0&&last==1)
{
temp--;
break;
}
else if(c==1&&last==0)
{
temp++;
break;
}
}
fout<<temp;
}