Submission #714848

# Submission time Handle Problem Language Result Execution time Memory
714848 2023-03-25T10:50:37 Z four_specks Sails (IOI07_sails) C++17
100 / 100
24 ms 3096 KB
#include <bits/stdc++.h>

using namespace std;

namespace
{
} // namespace

void solve()
{
    int n;
    cin >> n;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];

    const int X = *max_element(a.begin(), a.end());

    vector<long> run(X + 1);
    for (int i = 0; i < n; i++)
    {
        run[a[i] - b[i]]++;
        run[a[i]]--;
    }
    for (int x = 0; x < X; x++)
        run[x + 1] += run[x];

    stack<pair<long, int>> st;
    for (int x = 0; x < X; x++)
    {
        long cur = run[x];
        int cnt = 1;

        while (!st.empty() && st.top().first <= (cur + st.top().first) / (cnt + 1))
        {
            auto [x, y] = st.top();
            st.pop();

            cur += x * y;
            cnt += y;
        }

        long q = cur / cnt, d = cur % cnt;

        if (d > 0)
            st.emplace(q + 1, d);
        st.emplace(q, cnt - d);
    }

    long res = 0;

    while (!st.empty())
    {
        auto [x, y] = st.top();
        st.pop();

        res += y * x * (x - 1) / 2;
    }

    cout << res << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 716 KB Output is correct
2 Correct 5 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2268 KB Output is correct
2 Correct 16 ms 2300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 3096 KB Output is correct
2 Correct 20 ms 2008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2624 KB Output is correct
2 Correct 21 ms 2380 KB Output is correct