Cod sursa(job #2066358)

Utilizator DafinaTrufasTrufas Dafina DafinaTrufas Data 14 noiembrie 2017 22:11:01
Problema Transport Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int n, k, x[16001], max = 0, i, s = 0, t = 0, gasit = 0, j, z=0;
    ifstream f("transport.in");
    ofstream g("transport.out");
    f>> n >> k;
    for (i = 1; i <= n; i++)
        {
            f >> x[i];
            if (x[i] > max)
                max = x[i];
        }

   do{ t = 0; gasit = 0;s=0;
i=1;
do
{
    while (s < max && i<=n)
            s = s + x[i++];

    if (s == max)
        {
            t++;
            s = 0;
        }
    else if(s>max){t++;
          s = x[i-1];

          }
        else if (s<max && i==n)t++;

   }while(i<=n);
    if (s != 0)
            t++;

    if (t <= k)
            {g << max;
             gasit = 1;}
        else max++;

    } while (gasit == 0);

   f.close();
  g.close();
    return 0;
}