Submission #462057

# Submission time Handle Problem Language Result Execution time Memory
462057 2021-08-10T07:29:37 Z prvocislo Palembang Bridges (APIO15_bridge) C++17
100 / 100
293 ms 14384 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
typedef long long ll;
using namespace std;

multiset<int, greater<int>> small; multiset<int> big;
ll sumsmall = 0, sumbig = 0;
void addsmall(int x) { small.insert(x), sumsmall += x; }
void popsmall() { sumsmall -= *small.begin(), small.erase(small.begin()); }
void addbig(int x) { big.insert(x), sumbig += x; }
void popbig() { sumbig -= *big.begin(), big.erase(big.begin()); }
void insert(int x)
{
    int m = (big.empty() ? -1 : *big.begin());
    if (x < m) addsmall(x);
    else addbig(x);
    if (small.size() > big.size()) addbig(*small.begin()), popsmall();
    if (small.size() + 1 < big.size()) addsmall(*big.begin()), popbig();
}
struct segment { int l, r; };
vector<ll> solve(const vector<segment>& s) // l a r budu 1 indexovane
{
    int n = s.size() - 1;
    small.clear(), big.clear(), sumsmall = 0, sumbig = 0;
    vector<ll>ans(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        insert(s[i].l), insert(s[i].r);
        ll x = *prev(big.end());
        ans[i] = x * (ll)small.size() - sumsmall + sumbig - x * (ll)big.size();
    }
    return ans;
}
bool cmp(const segment& a, const segment& b) { return a.l + a.r < b.l + b.r; }
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int k, n;
    cin >> k >> n;
    vector<segment> v(1, { 0, 0 });
    ll sum = 0;
    for (int i = 0; i < n; i++)
    {
        char c1, c2; int l1, l2;
        cin >> c1 >> l1 >> c2 >> l2;
        if (c1 != c2) v.push_back({ l1, l2 }), sum++;
        else sum += abs(l1 - l2);
    }
    n = v.size()-1;
    sort(v.begin()+1, v.end(), cmp);
    vector<ll> ans1 = solve(v);
    if (k == 1)
    {
        cout << ans1[n] + sum << "\n";
        return 0;
    }
    reverse(v.begin() + 1, v.end());
    for (int i = 1; i <= n; i++) v[i] = { -v[i].r, -v[i].l };
    vector<ll> ans2 = solve(v);
    ll ans = 1e18;
    for (int i = 0; i <= n; i++)
    {
        ans = min(ans, ans1[i] + ans2[n - i]);
    }
    cout << ans + sum << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 2 ms 312 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 452 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 95 ms 11984 KB Output is correct
13 Correct 157 ms 13564 KB Output is correct
14 Correct 121 ms 11300 KB Output is correct
15 Correct 75 ms 8036 KB Output is correct
16 Correct 93 ms 12904 KB Output is correct
17 Correct 105 ms 13736 KB Output is correct
18 Correct 82 ms 13128 KB Output is correct
19 Correct 113 ms 13472 KB Output is correct
20 Correct 98 ms 13040 KB Output is correct
21 Correct 109 ms 13372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 272 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 444 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 3 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 189 ms 12752 KB Output is correct
26 Correct 242 ms 13000 KB Output is correct
27 Correct 268 ms 13784 KB Output is correct
28 Correct 267 ms 14272 KB Output is correct
29 Correct 293 ms 14376 KB Output is correct
30 Correct 110 ms 7832 KB Output is correct
31 Correct 160 ms 13628 KB Output is correct
32 Correct 180 ms 14384 KB Output is correct
33 Correct 139 ms 13904 KB Output is correct
34 Correct 172 ms 14384 KB Output is correct
35 Correct 178 ms 13928 KB Output is correct
36 Correct 177 ms 14076 KB Output is correct
37 Correct 157 ms 12952 KB Output is correct