Cod sursa(job #2923447)

Utilizator tallianfranciskaFranciska Tallian tallianfranciska Data 13 septembrie 2022 21:45:03
Problema Subsecventa de suma maxima Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.4 kb

#include <fstream>
#include <queue>
#include <algorithm>
#include <climits>
    using namespace std;
ifstream cin("ssm.in");
ofstream cout("ssm.out");
    vector<long long>x, y;
    long long i, n, d2, d;
    struct maxim
    {
        long long ertek, kezd, veg;
    };
    int main()
    {
        cin >> n;
        x.resize(n + 1);
        y.resize(n + 1);

        for (i = 1; i <= n; ++i)
        {
            cin >> x[i];
        }
        maxim maxi = { LLONG_MIN,0,0 };
        for (i = 1; i <= n; ++i)
        {
            d2 = d;
            if (d2 < 0)
            {
                d = x[i];
                y[i] = i;
            }
            else
            {
                d = x[i] + d2;
                y[i] = y[i - 1];
            }
            if (d > maxi.ertek || (d == maxi.ertek && i - y[i] < maxi.veg - maxi.kezd))
            {
                maxi.ertek = d;
                maxi.kezd = y[i];
                maxi.veg = i;
            }
        }
        cout << maxi.ertek << " " << maxi.kezd << " " << maxi.veg;

        return 0;
    }

    // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
    // Debug program: F5 or Debug > Start Debugging menu

    // Tips for Getting Started: 
    //   1. Use the Solution Explorer window to add/manage files
    //   2. Use the Team Explorer window to connect to source control
    //   3. Use the Output window to see build output and other messages
    //   4. Use the Error List window to view errors
    //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
    //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
    
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file