답안 #993743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993743 2024-06-06T10:55:06 Z NValchanov Tortoise (CEOI21_tortoise) C++17
100 / 100
43 ms 22988 KB
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long ll;

const ll MAXN = 1LL * (5e6 + 10LL);
const ll INF = 1LL * (4e18 + 10LL);

ll n;
ll a[MAXN];
ll lpg[MAXN];
ll rpg[MAXN];
ll after[MAXN];
ll sum;
ll breakpoint;

void read()
{
    cin >> n;
    for(int i = 1LL; i <= n; i++)
    {
        cin >> a[i];
        if(a[i] != -1LL)
            sum += a[i];
    }
}

void fill_pgs()
{
    breakpoint = -1LL;

    ll last = -1LL;
    for(ll i = 1LL; i <= n; i++)
    {
        if(a[i] == -1LL)
            last = i;
        else if(last != -1LL)
            lpg[i] = last;
    }

    last = -1LL;
    ll last2 = -1LL;

    for(ll i = n; i >= 1LL; i--)
    {
        if(a[i] == -1LL)
            last = i;
        else if(a[i])
        {
            if(last != -1)
                rpg[i] = last;

            if(last2 != -1)
                after[i] = last2;
            else
                breakpoint = i;

            last2 = i;
        }
    }
}

void solve()
{
    fill_pgs();

    ll ans = 0LL;

    map < ll, ll > mp;

    for(ll i = 1; i <= n; i++)
    {
        if(a[i] <= 0)
            continue;

        ll tim = INF;

        if(lpg[i])
            tim = min(tim, 1LL * 2LL * (i - lpg[i]));
        if(rpg[i])
            tim = min(tim, 1LL * 2LL * (rpg[i] - i));

        ll tmp = tim;

        if(after[i] && rpg[i])
        {
            tmp = (after[i] < rpg[i] ? min(tmp, 1LL * 2LL * (rpg[i] - after[i])) : 0LL);
        }

        if(i == breakpoint)
            tmp = 0LL;

        a[i]--;

        while(a[i] && ans <= i - 1)
        {
            mp[tim]++;
            ans += tim;
            a[i]--;
        }

        if(a[i])
        {
            if(prev(mp.end()) -> first >= tim)
            {
                mp[tim] += a[i];
                ans += (tim  * a[i]);
            }

            while(!mp.empty() && ans > i - 1)
            {
                auto [x, y] = *prev(mp.end());

                if(ans - 1 * x * y > i - 1)
                {
                    ans -= 1LL * (x * y);
                    mp.erase(prev(mp.end()));
                }
                else
                {
                    ll diff = ans - (i - 1LL);
                    ll z = 1LL * (diff - 1LL) / x;

                    ans -= 1LL * (x * z);
                    mp[x] -= z;
                    break;
                }
            }
        }

        if(ans > i - 1)
        {
            if(prev(mp.end()) -> first >= tmp)
            {
                auto [x, y] = *prev(mp.end());

                ans -= x;
                mp[x]--;

                if(!mp[x])
                    mp.erase(prev(mp.end()));
                
                ans += tmp;
                mp[tmp]++;
            }
        }
        else
        {
            ans += tmp;
            mp[tmp]++;
        }
    }

    for(auto it = mp.begin(); it != mp.end(); it++)
    {
        sum -= (it -> second);
    }

    cout << sum << endl;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6616 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6616 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6488 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6616 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6616 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6488 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6616 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6488 KB Output is correct
45 Correct 1 ms 6488 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6616 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 1 ms 6492 KB Output is correct
53 Correct 1 ms 6492 KB Output is correct
54 Correct 1 ms 6492 KB Output is correct
55 Correct 1 ms 6488 KB Output is correct
56 Correct 1 ms 6492 KB Output is correct
57 Correct 1 ms 6492 KB Output is correct
58 Correct 1 ms 6748 KB Output is correct
59 Correct 1 ms 6624 KB Output is correct
60 Correct 1 ms 6492 KB Output is correct
61 Correct 1 ms 6492 KB Output is correct
62 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6616 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6488 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6616 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6488 KB Output is correct
45 Correct 1 ms 6488 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6616 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 1 ms 6492 KB Output is correct
53 Correct 1 ms 6492 KB Output is correct
54 Correct 1 ms 6492 KB Output is correct
55 Correct 1 ms 6488 KB Output is correct
56 Correct 1 ms 6492 KB Output is correct
57 Correct 1 ms 6492 KB Output is correct
58 Correct 1 ms 6748 KB Output is correct
59 Correct 1 ms 6624 KB Output is correct
60 Correct 1 ms 6492 KB Output is correct
61 Correct 1 ms 6492 KB Output is correct
62 Correct 1 ms 6492 KB Output is correct
63 Correct 2 ms 8536 KB Output is correct
64 Correct 1 ms 8536 KB Output is correct
65 Correct 2 ms 8540 KB Output is correct
66 Correct 2 ms 8540 KB Output is correct
67 Correct 1 ms 8540 KB Output is correct
68 Correct 1 ms 8540 KB Output is correct
69 Correct 1 ms 8536 KB Output is correct
70 Correct 1 ms 8540 KB Output is correct
71 Correct 2 ms 8540 KB Output is correct
72 Correct 1 ms 8676 KB Output is correct
73 Correct 2 ms 8536 KB Output is correct
74 Correct 2 ms 8540 KB Output is correct
75 Correct 1 ms 8540 KB Output is correct
76 Correct 1 ms 8540 KB Output is correct
77 Correct 1 ms 8540 KB Output is correct
78 Correct 1 ms 8540 KB Output is correct
79 Correct 1 ms 8540 KB Output is correct
80 Correct 2 ms 8540 KB Output is correct
81 Correct 2 ms 8540 KB Output is correct
82 Correct 2 ms 8540 KB Output is correct
83 Correct 1 ms 8540 KB Output is correct
84 Correct 1 ms 8540 KB Output is correct
85 Correct 1 ms 8680 KB Output is correct
86 Correct 2 ms 8540 KB Output is correct
87 Correct 1 ms 8540 KB Output is correct
88 Correct 1 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 6488 KB Output is correct
20 Correct 1 ms 6616 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6488 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6616 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6488 KB Output is correct
45 Correct 1 ms 6488 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6616 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 1 ms 6492 KB Output is correct
53 Correct 1 ms 6492 KB Output is correct
54 Correct 1 ms 6492 KB Output is correct
55 Correct 1 ms 6488 KB Output is correct
56 Correct 1 ms 6492 KB Output is correct
57 Correct 1 ms 6492 KB Output is correct
58 Correct 1 ms 6748 KB Output is correct
59 Correct 1 ms 6624 KB Output is correct
60 Correct 1 ms 6492 KB Output is correct
61 Correct 1 ms 6492 KB Output is correct
62 Correct 1 ms 6492 KB Output is correct
63 Correct 2 ms 8536 KB Output is correct
64 Correct 1 ms 8536 KB Output is correct
65 Correct 2 ms 8540 KB Output is correct
66 Correct 2 ms 8540 KB Output is correct
67 Correct 1 ms 8540 KB Output is correct
68 Correct 1 ms 8540 KB Output is correct
69 Correct 1 ms 8536 KB Output is correct
70 Correct 1 ms 8540 KB Output is correct
71 Correct 2 ms 8540 KB Output is correct
72 Correct 1 ms 8676 KB Output is correct
73 Correct 2 ms 8536 KB Output is correct
74 Correct 2 ms 8540 KB Output is correct
75 Correct 1 ms 8540 KB Output is correct
76 Correct 1 ms 8540 KB Output is correct
77 Correct 1 ms 8540 KB Output is correct
78 Correct 1 ms 8540 KB Output is correct
79 Correct 1 ms 8540 KB Output is correct
80 Correct 2 ms 8540 KB Output is correct
81 Correct 2 ms 8540 KB Output is correct
82 Correct 2 ms 8540 KB Output is correct
83 Correct 1 ms 8540 KB Output is correct
84 Correct 1 ms 8540 KB Output is correct
85 Correct 1 ms 8680 KB Output is correct
86 Correct 2 ms 8540 KB Output is correct
87 Correct 1 ms 8540 KB Output is correct
88 Correct 1 ms 8540 KB Output is correct
89 Correct 26 ms 22216 KB Output is correct
90 Correct 29 ms 22364 KB Output is correct
91 Correct 28 ms 22096 KB Output is correct
92 Correct 32 ms 22096 KB Output is correct
93 Correct 40 ms 22100 KB Output is correct
94 Correct 32 ms 22100 KB Output is correct
95 Correct 29 ms 22100 KB Output is correct
96 Correct 30 ms 22096 KB Output is correct
97 Correct 32 ms 22104 KB Output is correct
98 Correct 43 ms 22100 KB Output is correct
99 Correct 43 ms 22988 KB Output is correct
100 Correct 41 ms 22616 KB Output is correct
101 Correct 41 ms 22608 KB Output is correct
102 Correct 41 ms 22620 KB Output is correct
103 Correct 19 ms 17844 KB Output is correct
104 Correct 4 ms 10840 KB Output is correct
105 Correct 3 ms 10844 KB Output is correct
106 Correct 4 ms 10844 KB Output is correct
107 Correct 4 ms 10964 KB Output is correct
108 Correct 3 ms 10844 KB Output is correct
109 Correct 19 ms 17756 KB Output is correct