77882024-01-11 10:09:52tamasmarkSípálya (55 pont)cpp17Wrong answer 41/5597ms19172 KiB
// sipalya'.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <deque>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

int main()
{
    long long n, k;
    cin >> n >> k;
    vector<pair<long long,long long>>x(n + 1);
    priority_queue<pair<long long, long long>>p,p2;
    long long s1,s2,s3,mini=LONG_MAX,kulombseg;
    for (int i = 1; i <= n; ++i)
    {
        cin >> x[i].first;

        x[i].second = x[i - 1].second + x[i].first;
        p.push({ x[i].first,i });
        while (!p.empty() && p.top().second <= i - k)
        {
            p.pop();
        }
        if (p.top().first-x[i].first<i-p.top().second)
        {
            p2.push({ x[i].first,i });
            
        }
        while (!p2.empty() && p2.top().second <= i - k)
        {
            p2.pop();
        }
        if(!p2.empty())
        {
            if(p.top().first==p2.top().first&&p.top().second==p2.top().second) p2.pop();
            if(p.top().second>p2.top().second) p2.pop();
        }

        if (i >= k)
        {
            x[i].second -= x[i - k].first;
            s1 = (p.top().second - (i - k + 1)) + p.top().first;
            if(!p2.empty()) s2 = p.top().first - ((p2.top().second-1) - p.top().second);
            else s2 = p.top().first - (i - p.top().second);
            if (!p2.empty())
            {
                kulombseg = p2.top().first - s2+1;
                s2 = p2.top().first - (i - p2.top().second);
                s1 += kulombseg;
                s3 = (((s1 + s2) * k) / 2)-x[i].second;
                //cout<<s3<<"\n";
            }
            else
            {
                 s3 = (((s1 + s2)*k)/2)-x[i].second;
                 //cout<<s3<<"\n";
            }
            mini = min(mini, s3);
        }
    }
    cout << mini;
    return 0;
}
/*
5 3
5 5 6 3 1
*/
// 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
SubtaskSumTestVerdictTimeMemory
base41/55
1Accepted0/03ms1808 KiB
2Accepted0/03ms2000 KiB
3Accepted2/23ms2244 KiB
4Accepted2/23ms2604 KiB
5Accepted2/23ms2812 KiB
6Accepted2/23ms3028 KiB
7Accepted3/33ms3088 KiB
8Wrong answer0/17ms3952 KiB
9Wrong answer0/17ms4096 KiB
10Wrong answer0/17ms3908 KiB
11Accepted1/17ms3496 KiB
12Accepted1/17ms3588 KiB
13Accepted1/17ms3844 KiB
14Accepted2/27ms3928 KiB
15Accepted2/27ms3728 KiB
16Accepted2/293ms13512 KiB
17Accepted2/286ms17672 KiB
18Wrong answer0/286ms17796 KiB
19Wrong answer0/390ms19172 KiB
20Accepted2/290ms17724 KiB
21Accepted2/290ms17848 KiB
22Accepted2/293ms17828 KiB
23Accepted2/293ms17884 KiB
24Accepted2/297ms17820 KiB
25Accepted2/293ms17924 KiB
26Accepted2/290ms17824 KiB
27Accepted2/287ms17824 KiB
28Wrong answer0/387ms18068 KiB
29Accepted3/387ms17836 KiB
30Wrong answer0/390ms18220 KiB