Submission #935106

# Submission time Handle Problem Language Result Execution time Memory
935106 2024-02-28T15:57:00 Z LonlyR Palembang Bridges (APIO15_bridge) C++17
100 / 100
127 ms 7796 KB
#include<bits/stdc++.h>
#define int long long

using namespace std;
const int maxn = 1e5 + 5;
int n, same, k, sumn, sumx, cur;
int pref[maxn];
vector<pair<int,int>> points;
priority_queue<int> mn;
priority_queue<int, vector<int>, greater<int>> mx;

inline void amn(int x)
{
    mn.emplace(x);
    sumn += x;
}

inline void amx(int x)
{
    mx.emplace(x);
    sumx += x;
}

inline void popn()
{
    sumn -= mn.top();
    mn.pop();
}

inline void popx()
{
    sumx -= mx.top();
    mx.pop();
}

inline void add(int x)
{
    if (mn.size() == mx.size()) 
    {
        if (mn.empty())
            amn(x);
        else
        {
            vector<int> tmp = {x, mn.top(), mx.top()};
            popn(); popx();
            sort(tmp.begin(), tmp.end());
            amn(tmp[0]);
            amn(tmp[1]);
            amx(tmp[2]);
        }
    }
    else
    {
        if (mx.empty())
        {
            vector<int> tmp = {mn.top(), x};
            popn();
            sort(tmp.begin(), tmp.end());
            amn(tmp[0]);
            amx(tmp[1]);
        }
        else
        {
            vector<int> tmp = {x, mn.top(), mx.top()};
            popn(); popx();
            sort(tmp.begin(), tmp.end());
            amn(tmp[0]);
            amx(tmp[1]);
            amx(tmp[2]);
        }
    }
    if (mn.size() == mx.size())
    {
        int median = (mn.top() + mx.top()) / 2;
        cur = sumx - sumn;
    }
}

bool cmp(pair<int,int> a, pair<int,int> b)
{
    return a.first + a.second < b.first + b.second;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    cin >> k >> n;
    for (int i = 1; i <= n; i++)
    {
        char side1, side2;
        int x, y;
        cin >> side1 >> x >> side2 >> y;
        if (side1 == side2) same += abs(x - y);
        else points.emplace_back(x, y);
    }
    n = points.size();
    if (!n) return cout << same, 0;
    sort(points.begin(), points.end(), cmp);
    for (int i = 0; i < n; i++)
    {
        add(points[i].first);
        add(points[i].second);
        pref[i] = cur;
    }
    if (k == 1) return cout << pref[n - 1] + n + same, 0;
    int ans = pref[n - 1];
    while (mn.size()) popn();
    while (mx.size()) popx();
    for (int i = n - 1; i >= 1; i--)
    {
        add(points[i].first);
        add(points[i].second);
        ans = min(ans, pref[i - 1] + cur);
    }
    cout << ans + n + same;
}

Compilation message

bridge.cpp: In function 'void add(long long int)':
bridge.cpp:74:13: warning: unused variable 'median' [-Wunused-variable]
   74 |         int median = (mn.top() + mx.top()) / 2;
      |             ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 472 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 33 ms 5712 KB Output is correct
13 Correct 67 ms 5572 KB Output is correct
14 Correct 57 ms 5320 KB Output is correct
15 Correct 40 ms 3020 KB Output is correct
16 Correct 34 ms 5648 KB Output is correct
17 Correct 55 ms 5632 KB Output is correct
18 Correct 56 ms 5548 KB Output is correct
19 Correct 63 ms 5704 KB Output is correct
20 Correct 41 ms 5576 KB Output is correct
21 Correct 59 ms 5612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 532 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 376 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 464 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 56 ms 6356 KB Output is correct
26 Correct 103 ms 6520 KB Output is correct
27 Correct 126 ms 7368 KB Output is correct
28 Correct 127 ms 7752 KB Output is correct
29 Correct 121 ms 7796 KB Output is correct
30 Correct 65 ms 4004 KB Output is correct
31 Correct 52 ms 6952 KB Output is correct
32 Correct 100 ms 7648 KB Output is correct
33 Correct 103 ms 7364 KB Output is correct
34 Correct 106 ms 7368 KB Output is correct
35 Correct 64 ms 6924 KB Output is correct
36 Correct 102 ms 7364 KB Output is correct
37 Correct 46 ms 6292 KB Output is correct