77362024-01-10 19:47:17tamasmarkSípálya (55 pont)cpp17Hibás válasz 37/55103ms21232 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;
struct adat
{
    long long first,second,third;
};
bool operator<(const adat a, const adat b)
{
    if(a.third==b.third) return a.first<b.first;
    else return a.third<b.third;
}

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;
    priority_queue<adat>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,p.top().first-x[i].first+i-p.top().second});
            
        }
        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())
            {
                //cout<<p2.top().first;
                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
RészfeladatÖsszpontTesztVerdiktIdőMemória
base37/55
1Elfogadva0/03ms1956 KiB
2Elfogadva0/03ms2216 KiB
3Elfogadva2/23ms2464 KiB
4Elfogadva2/23ms2508 KiB
5Elfogadva2/23ms2700 KiB
6Elfogadva2/23ms2656 KiB
7Elfogadva3/33ms2780 KiB
8Hibás válasz0/17ms3716 KiB
9Hibás válasz0/17ms4012 KiB
10Hibás válasz0/17ms4064 KiB
11Elfogadva1/17ms3712 KiB
12Elfogadva1/17ms4200 KiB
13Elfogadva1/17ms4372 KiB
14Elfogadva2/27ms4348 KiB
15Elfogadva2/27ms4212 KiB
16Elfogadva2/297ms14000 KiB
17Elfogadva2/290ms18152 KiB
18Hibás válasz0/290ms18488 KiB
19Hibás válasz0/396ms21232 KiB
20Hibás válasz0/293ms18868 KiB
21Elfogadva2/294ms18496 KiB
22Elfogadva2/297ms18380 KiB
23Hibás válasz0/297ms18656 KiB
24Elfogadva2/2103ms18476 KiB
25Elfogadva2/294ms18608 KiB
26Elfogadva2/293ms18724 KiB
27Elfogadva2/293ms18804 KiB
28Hibás válasz0/392ms19200 KiB
29Elfogadva3/392ms18808 KiB
30Hibás válasz0/393ms19264 KiB