Submission #709600

# Submission time Handle Problem Language Result Execution time Memory
709600 2023-03-14T01:51:47 Z nguyentunglam Palembang Bridges (APIO15_bridge) C++17
100 / 100
94 ms 6076 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
pair<int, int> a[N];
long long sum, pref[N], suff[N];
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
void balance() {
    if (q1.top() > q2.top()) {
        int x = q1.top(), y = q2.top();
        q1.pop(); q2.pop();
        sum += 2 * x; sum -= 2 * y;
        q1.push(y); q2.push(x);
    }
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int k, n, m = 0; cin >> k >> n;
    long long sameside = 0;
    for(int i = 1; i <= n; i++) {
        char p, q;
        int s, t;
        cin >> p >> s >> q >> t;
        if (p == q) sameside += abs(s - t);
        else {
            if (s > t) swap(s, t);
            a[++m] = make_pair(s, t);
            sameside++;
        }
    }
    sort(a + 1, a + m + 1, [] (const ii &x, const ii &y) {
         return x.fi + x.se < y.fi + y.se;
         });
 
    for(int i = 1; i <= m; i++) {
        q1.push(a[i].fi); sum -= a[i].fi;
        q2.push(a[i].se); sum += a[i].se;
        balance();
        pref[i] = sum;
    }
    if (k == 1) return cout << pref[m] + sameside, 0;
    sum = 0;
    while (!q1.empty()) q1.pop();
    while (!q2.empty()) q2.pop();
    for(int i = m; i >= 1; i--) {
        q1.push(a[i].fi); sum -= a[i].fi;
        q2.push(a[i].se); sum += a[i].se;
        balance();
        suff[i] = sum;
    }
    long long res = pref[m];
    for(int i = 2; i <= m; i++) res = min(res, suff[i] + pref[i - 1]);
    cout << res + sameside;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:24:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:25:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:28:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:29:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 476 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 22 ms 3608 KB Output is correct
13 Correct 48 ms 5108 KB Output is correct
14 Correct 37 ms 3780 KB Output is correct
15 Correct 28 ms 3272 KB Output is correct
16 Correct 27 ms 4440 KB Output is correct
17 Correct 55 ms 5184 KB Output is correct
18 Correct 29 ms 4752 KB Output is correct
19 Correct 54 ms 5148 KB Output is correct
20 Correct 34 ms 4672 KB Output is correct
21 Correct 49 ms 4844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 372 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 224 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 356 KB Output is correct
25 Correct 31 ms 4416 KB Output is correct
26 Correct 64 ms 4604 KB Output is correct
27 Correct 94 ms 5376 KB Output is correct
28 Correct 87 ms 5984 KB Output is correct
29 Correct 84 ms 6076 KB Output is correct
30 Correct 44 ms 3420 KB Output is correct
31 Correct 37 ms 5420 KB Output is correct
32 Correct 81 ms 5904 KB Output is correct
33 Correct 50 ms 5604 KB Output is correct
34 Correct 83 ms 5980 KB Output is correct
35 Correct 48 ms 5464 KB Output is correct
36 Correct 79 ms 5728 KB Output is correct
37 Correct 23 ms 4332 KB Output is correct