Cod sursa(job #1844268)

Utilizator zanescucristianZanescu Cristian zanescucristian Data 9 ianuarie 2017 20:57:17
Problema Orase Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <stdlib.h>
#include <fstream>
#include <map>
using namespace std;

#define MAX_ORASE   50000

int main()
{
    int lngStradaPrinc,nrOrase,d=0,lungimeMaxima=0;
    int i,j,li,di;
    ifstream in ("orase.in");
    ofstream out ("orase.out");
    in >> lngStradaPrinc >> nrOrase;

    if (nrOrase <= MAX_ORASE)
    {
        bool use_maps = true;
        if (use_maps)
        {
            multimap<int,int> tabel;
            for (i = 0; i < nrOrase; ++i)
            {
                in >> di >> li;
                tabel.insert( pair<int,int>( li, di) );
            }

            int lj, lMin = 0;
            multimap<int,int>::reverse_iterator it1 = tabel.rbegin();
            multimap<int,int>::reverse_iterator it2 = it1;
            if (tabel.size() >= 2)
            {
                ++it2;
                lMin = it2->first - lngStradaPrinc;
            }

            for (; it1 != tabel.rend(); ++it1)
            {
                li = it1->first;
                if (li < lMin) { break; }

                di = it1->second;

                it2 = it1;
                ++it2;
                for ( ; it2 != tabel.rend(); ++it2)
                {
                    lj = it2->first;
                    if (lj < lMin) { break; }
                    d = li + lj + abs(di - it2->second);
                    if (lungimeMaxima < d) { lungimeMaxima = d; }
                }
            }
        }
        else
        {
            int distanta[MAX_ORASE];
            int lungime [MAX_ORASE];
            for (i = 0; i < nrOrase; ++i)
            {
                in >> di >> li;
                distanta[i] = di;
                lungime[i]  = li;
                for (j = 0; j < i; ++j)
                {
                    d = li + lungime[j] + abs(di - distanta[j]);
                    if (lungimeMaxima < d) { lungimeMaxima = d; }
                }
            }
        }

        out<<lungimeMaxima;
    }
    else
    {
        out<<"Nr orase prea mare";   // error message
    }
    return 0;
}