Submission #47563

# Submission time Handle Problem Language Result Execution time Memory
47563 2018-05-05T03:49:37 Z kuroni Schools (IZhO13_school) C++14
100 / 100
174 ms 8152 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int N = 300005;

struct SCity
{
    int a, b;
} s[N];
int n;
unsigned int a, b;
long long pre[N], suf[N];
priority_queue<int, vector<int>, greater<int>> pq;

int main()
{
    scanf("%d%u%u", &n, &a, &b);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &s[i].a, &s[i].b);
    sort(s + 1, s + n + 1, [](const SCity &a, const SCity &b) { return a.a - a.b > b.a - b.b; });
    for (int i = 1; i <= n; i++)
    {
        if (pq.size() < a)
        {
            pre[i] = pre[i - 1] + s[i].a;
            pq.push(s[i].a);
        }
        else if (pq.top() < s[i].a)
        {
            pre[i] = pre[i - 1] - pq.top() + s[i].a;
            pq.pop();
            pq.push(s[i].a);
        }
        else
            pre[i] = pre[i - 1];
    }
    while (!pq.empty())
        pq.pop();
    for (int i = n; i >= 1; i--)
    {
        if (pq.size() < b)
        {
            suf[i] = suf[i + 1] + s[i].b;
            pq.push(s[i].b);
        }
        else if (pq.top() < s[i].b)
        {
            suf[i] = suf[i + 1] - pq.top() + s[i].b;
            pq.pop();
            pq.push(s[i].b);
        }
        else
            suf[i] = suf[i + 1];
    }
    while (!pq.empty())
        pq.pop();
    long long ans = 0;
    for (int i = 0; i <= n; i++)
        ans = max(ans, pre[i] + suf[i + 1]);
    printf("%lld", ans);
}

Compilation message

school.cpp: In function 'int main()':
school.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%u%u", &n, &a, &b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
school.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &s[i].a, &s[i].b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 480 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 3 ms 824 KB Output is correct
8 Correct 4 ms 848 KB Output is correct
9 Correct 4 ms 848 KB Output is correct
10 Correct 4 ms 848 KB Output is correct
11 Correct 4 ms 848 KB Output is correct
12 Correct 4 ms 848 KB Output is correct
13 Correct 18 ms 1636 KB Output is correct
14 Correct 34 ms 2660 KB Output is correct
15 Correct 73 ms 4452 KB Output is correct
16 Correct 94 ms 5592 KB Output is correct
17 Correct 105 ms 6276 KB Output is correct
18 Correct 110 ms 6748 KB Output is correct
19 Correct 119 ms 7232 KB Output is correct
20 Correct 174 ms 8152 KB Output is correct