Submission #1040429

#TimeUsernameProblemLanguageResultExecution timeMemory
1040429Yang8onSchools (IZhO13_school)C++14
100 / 100
90 ms13840 KiB
#include <bits/stdc++.h>
#define Y8o "Schools"
#define maxn (int) 3e5 + 5
#define ll long long
#define pii pair<ll, ll>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r

#define f first
#define s second

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
    if(fopen(Y8o".in", "r"))
    {
        freopen(Y8o".in", "r", stdin);
        freopen(Y8o".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
    cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}


int n, m, t;
pii a[maxn];
ll dp[maxn][2];

void solve()
{
    cin >> n >> m >> t;
    for(int i = 1; i <= n; i ++) cin >> a[i].f >> a[i].s;

    sort(a + 1, a + n + 1, [](pii x, pii y) {
           return x.f - x.s == y.f - y.s ? x.f > y.f : x.f - x.s > y.f - y.s;
    });

    ll sum = 0;
    priority_queue<int> q;

    for(int i = 1; i <= n; i ++) {
        q.push(-a[i].f), sum += a[i].f;
        if(q.size() < m) dp[i][0] = -1e18;
        else if(q.size() == m) dp[i][0] = sum;
        else sum -= -q.top(), q.pop(), dp[i][0] = sum;
        dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }
    while(q.size()) q.pop();

    sum = 0;
    for(int i = n; i >= 1; i --) {
        q.push(-a[i].s), sum += a[i].s;
        if(q.size() < t) dp[i][1] = -1e18;
        else if(q.size() == t) dp[i][1] = sum;
        else sum -= -q.top(), q.pop(), dp[i][1] = sum;
        dp[i][1] = max(dp[i][1], dp[i + 1][1]);
    }

    ll ans = 0;
    for(int i = 0; i <= n; i ++) {
        ans = max(ans, dp[i][0] + dp[i + 1][1]);
    }

    cout << ans;
}


int main()
{
    iof();

    int nTest = 1;
//    cin >> nTest;

    while(nTest --) {
        solve();
    }

    ctime();
    return 0;
}

Compilation message (stderr)

school.cpp: In function 'void solve()':
school.cpp:52:21: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |         if(q.size() < m) dp[i][0] = -1e18;
      |            ~~~~~~~~~^~~
school.cpp:53:26: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   53 |         else if(q.size() == m) dp[i][0] = sum;
      |                 ~~~~~~~~~^~~~
school.cpp:62:21: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         if(q.size() < t) dp[i][1] = -1e18;
      |            ~~~~~~~~~^~~
school.cpp:63:26: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   63 |         else if(q.size() == t) dp[i][1] = sum;
      |                 ~~~~~~~~~^~~~
school.cpp: In function 'void iof()':
school.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen(Y8o".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
school.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen(Y8o".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...