Submission #533905

# Submission time Handle Problem Language Result Execution time Memory
533905 2022-03-07T15:19:54 Z Bungmint Palembang Bridges (APIO15_bridge) C++17
100 / 100
358 ms 13608 KB
//Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda{
	template <typename Fun>
	struct y_combinator_result{
		Fun fun_;
		template <typename T> 
		explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
		template <typename...Args>
		decltype(auto) operator()(Args &&...args){
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun){
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

void setIO(string s) // USACO
{
	#ifndef LOCAL
	    freopen((s + ".in").c_str(), "r", stdin);
	    freopen((s + ".out").c_str(), "w", stdout);
	#endif
}

struct TrackMedian{
	multiset<int> low, high;
	ll sumLow{}, sumHigh{};
	void add(int x) {
		if (sz(low) == 0) {
			sumLow += x;
			low.insert(x);
			return;
		}
		if (x > *low.rbegin()) {
			high.insert(x);
			sumHigh += x;
			if (sz(high) > sz(low)) {
				sumHigh -= *high.begin();
				sumLow += *high.begin();
				low.insert(*high.begin()), high.erase(high.begin());
			}
		}else{
			low.insert(x);
			sumLow += x;
			if (sz(high) + 1 < sz(low)) {
				sumHigh += *low.rbegin();
				sumLow -= *low.rbegin();
				high.insert(*low.rbegin());
				low.erase(--low.end());
			}
		}
	}
	void remove(int x) {
		if (x > *low.rbegin()) {
			high.erase(high.find(x));
			sumHigh -= x;
			if (sz(high) + 1 < sz(low)) {
				sumHigh += *low.rbegin();
				sumLow -= *low.rbegin();
				high.insert(*low.rbegin());
				low.erase(--low.end());
			}
		}else{
			low.erase(low.find(x));
			sumLow -= x;
			if (sz(high) > sz(low)) {
				sumHigh -= *high.begin();
				sumLow += *high.begin();
				low.insert(*high.begin()), high.erase(high.begin());
			}
		}
	}
	ll calc() {
		if (!sz(low)) return 0LL;
		int med = *low.rbegin();
		return sumHigh - 1LL * med * sz(high) + 1LL * med * sz(low) - sumLow;
	}
};

void solve()
{
	int k, n;
	cin >> k >> n;
	ll ans{};
	ll extra = LINF;
	vi order;
	vpi a;
	F0R(i, n) {
		char c, d;
		int l, r;
		cin >> c >> l >> d >> r;
		if (c != d)	order.pb(l), order.pb(r), a.pb({l, r}), ans++;
		else ans += abs(r - l);
	}
	if (k == 1) {
		sort(all(order));
		if (sz(order)) {
			int med = order[sz(order) / 2];
			for (auto &[l, r] : a) ans += abs(l - med) + abs(r - med);
		}
		cout << ans;
	}else{
		sort(all(a), [&](pii p, pii q) {
			return (p.fi + p.se) < (q.fi + q.se);
		});
		TrackMedian lo, hi;
		for (auto &[l, r] : a) hi.add(l), hi.add(r);
		ckmin(extra, hi.calc());
		F0R(i, sz(a)) {
			hi.remove(a[i].fi), hi.remove(a[i].se);
			lo.add(a[i].fi), lo.add(a[i].se);
			while (i + 1 < sz(a) && (a[i + 1].fi + a[i + 1].se) == a[i].fi + a[i].se) {
				i++;
				hi.remove(a[i].fi), hi.remove(a[i].se);
				lo.add(a[i].fi), lo.add(a[i].se);
			}
			ckmin(extra, hi.calc() + lo.calc());
		}
		cout << ans + extra;
	}
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

Compilation message

bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |      freopen((s + ".in").c_str(), "r", stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |      freopen((s + ".out").c_str(), "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 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 332 KB Output is correct
9 Correct 1 ms 356 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 204 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 356 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 20 ms 2784 KB Output is correct
13 Correct 41 ms 4296 KB Output is correct
14 Correct 35 ms 3128 KB Output is correct
15 Correct 26 ms 2708 KB Output is correct
16 Correct 31 ms 3752 KB Output is correct
17 Correct 33 ms 4524 KB Output is correct
18 Correct 35 ms 4012 KB Output is correct
19 Correct 39 ms 4408 KB Output is correct
20 Correct 38 ms 3920 KB Output is correct
21 Correct 42 ms 4140 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 332 KB Output is correct
4 Correct 1 ms 320 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 332 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 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 0 ms 312 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 312 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 328 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 324 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 2 ms 324 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 1 ms 464 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 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 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 1 ms 320 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 328 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 172 ms 11984 KB Output is correct
26 Correct 276 ms 12184 KB Output is correct
27 Correct 356 ms 12988 KB Output is correct
28 Correct 324 ms 13488 KB Output is correct
29 Correct 358 ms 13480 KB Output is correct
30 Correct 120 ms 7220 KB Output is correct
31 Correct 141 ms 12836 KB Output is correct
32 Correct 186 ms 13532 KB Output is correct
33 Correct 132 ms 13128 KB Output is correct
34 Correct 197 ms 13608 KB Output is correct
35 Correct 195 ms 13100 KB Output is correct
36 Correct 182 ms 13312 KB Output is correct
37 Correct 202 ms 12004 KB Output is correct