Submission #1024910

#TimeUsernameProblemLanguageResultExecution timeMemory
1024910c2zi6Wiring (IOI17_wiring)C++14
13 / 100
24 ms5496 KiB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "wiring.h"

namespace TEST2 {
    ll min_total_length(VI a, VI b) {
        ll ans = 0;
        int l = a.size()-1;
        int r = 0;
        while (l >= 0 && r < b.size()) {
            ans += b[r] - a[l];
            l--;
            r++;
        }
        while (l >= 0) ans += b[0] - a[l--];
        while (r < b.size()) ans += b[r++] - a.back();
        return ans;
    }
};

ll min_total_length(VI A, VI B) {
    VPI a;
    for (int x : A) a.pb({x, 0});
    for (int x : B) a.pb({x, 1});
    sort(all(a));
    ll ans = 0;
    stack<PII> st;
    for (auto&[x, type] : a) {
        if (st.size() == 0 || st.top().ss == type) {
            st.push({x, type});
        } else {
            ans += x - st.top().ff;
            st.pop();
        }
    }

    auto nearest = [&](VI& a, int x) {
        int ret = 2e9;
        auto it = upper_bound(all(a), x);
        if (it != a.end()) setmin(ret, *it - x);
        it = lower_bound(all(a), x);
        if (it != a.begin()) setmin(ret, x - *prev(it));
        return ret;
    };

    while (st.size()) {
        auto&[x, type]  = st.top();
        if (type == 0) ans += nearest(B, x);
        else ans += nearest(A, x);
        st.pop();
    }

	return ans;
}

Compilation message (stderr)

wiring.cpp: In function 'll TEST2::min_total_length(VI, VI)':
wiring.cpp:39:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         while (l >= 0 && r < b.size()) {
      |                          ~~^~~~~~~~~~
wiring.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         while (r < b.size()) ans += b[r++] - a.back();
      |                ~~^~~~~~~~~~
wiring.cpp: In function 'll min_total_length(VI, VI)':
wiring.cpp:57:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   57 |     for (auto&[x, type] : a) {
      |               ^
wiring.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |         auto&[x, type]  = st.top();
      |              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...