Cod sursa(job #2254781)

Utilizator ToduczToducz Endre Toducz Data 5 octombrie 2018 22:51:32
Problema Transport Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <iostream>
#include <fstream>
using namespace std;

ofstream ki("transport.out");

void read (int t[], int &n, int &db, int& my_sum, int& my_max)
{
    ifstream be("transport.in");
    be >> n;
    be >> db;
    be >>  my_max;
    t[1] =   my_max;
    my_sum =  my_max;

    for (int i = 2; i <= n ; i++)
    {
        be >> t[i];
        my_max = max(t[i], my_max);
        my_sum += t[i];
    }

}




int test(int check_capacity, int db, int warehouse[], int n)
{
    int virtual_check_capacity = check_capacity, i;

    for(i = 1; i <= n && db > 0 ; i++)
    {

        if((virtual_check_capacity - warehouse[i]) >= 0)
            virtual_check_capacity = virtual_check_capacity - warehouse[i];
        else
        {
            db--;
            if( db < 1 or check_capacity - warehouse[i] < 0 )
                return -1;

            virtual_check_capacity = check_capacity - warehouse[i];
        }
    }

    return virtual_check_capacity;
}

void my_binary_search (int db, int warehouse[], int n, int first, int last)
{
    int checkNr;

    if(first == last)
        ki << first;
    else
    {
        checkNr = test((first + last) / 2, db,  warehouse, n);


        if (checkNr < 0)
            my_binary_search (db, warehouse, n, (first + last) / 2, last);
        else
            my_binary_search (db, warehouse, n, first, (first + last) / 2);
    }
}

int main()
{
    int warehouse[16001], n, db, first, last, my_sum = 0, my_max;

    read(warehouse, n, db, my_sum, my_max);

    my_binary_search(db, warehouse, n, max(my_sum / db, my_max), my_sum);
    //cout << test(121, 5, warehouse, n);
    ki.close();
}