답안 #435551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435551 2021-06-23T12:29:03 Z sinamhdv Palembang Bridges (APIO15_bridge) C++11
100 / 100
100 ms 3720 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

#define MAXN 100100

int k, n;
int lft[MAXN], rgt[MAXN];
char z1[MAXN], z2[MAXN];
ll ans, res[MAXN];
vector<int> vec;

priority_queue<int> pql;
priority_queue<int, vector<int>, greater<int>> pqr;
ll lsum, rsum;

void add(int x)
{
	if (pqr.size() && x >= pqr.top()) pqr.push(x), rsum += x;
	else pql.push(x), lsum += x;
	if (pqr.size() > pql.size())
	{
		pql.push(pqr.top());
		lsum += pqr.top();
		rsum -= pqr.top();
		pqr.pop();
	}
	else if (pql.size() > pqr.size() + 1)
	{
		pqr.push(pql.top());
		lsum -= pql.top();
		rsum += pql.top();
		pql.pop();
	}
}

inline ll getres(void)
{
	ll mid = pql.top();
	return mid * pql.size() - lsum + rsum - mid * pqr.size();
}

int32_t main(void)
{
	fast_io;
	cin >> k >> n;
	FOR(i, 0, n)
	{
		cin >> z1[i] >> lft[i] >> z2[i] >> rgt[i];
		if (lft[i] > rgt[i]) swap(lft[i], rgt[i]);
		if (z1[i] == z2[i])
			ans += rgt[i] - lft[i];
		else
			vec.pb(i), ans++;
	}

	sort(all(vec), [&](int x, int y){ return (lft[x] + rgt[x]) < (lft[y] + rgt[y]); });

	FOR(i, 1, vec.size())
	{
		add(lft[vec[i - 1]]);
		add(rgt[vec[i - 1]]);
		res[i] += getres();
	}

	// clear
	while (pql.size()) pql.pop();
	while (pqr.size()) pqr.pop();
	lsum = rsum = 0;

	for (int i = (int)vec.size() - 1; i >= 0; i--)
	{
		add(lft[vec[i]]);
		add(rgt[vec[i]]);
		res[i] += getres();
	}

	if (k == 2)
		cout << *min_element(res, res + vec.size()) + ans << endl;
	else cout << res[0] + ans << endl;

	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 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 332 KB Output is correct
8 Correct 1 ms 332 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 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 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 41 ms 3692 KB Output is correct
13 Correct 97 ms 3500 KB Output is correct
14 Correct 79 ms 3324 KB Output is correct
15 Correct 55 ms 2132 KB Output is correct
16 Correct 45 ms 3648 KB Output is correct
17 Correct 74 ms 3680 KB Output is correct
18 Correct 56 ms 3720 KB Output is correct
19 Correct 86 ms 3648 KB Output is correct
20 Correct 58 ms 3652 KB Output is correct
21 Correct 79 ms 3684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 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 332 KB Output is correct
8 Correct 1 ms 332 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 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 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 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 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 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 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 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 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 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 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 1 ms 332 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 41 ms 3628 KB Output is correct
26 Correct 70 ms 3528 KB Output is correct
27 Correct 93 ms 3648 KB Output is correct
28 Correct 100 ms 3528 KB Output is correct
29 Correct 99 ms 3648 KB Output is correct
30 Correct 58 ms 1904 KB Output is correct
31 Correct 43 ms 3652 KB Output is correct
32 Correct 74 ms 3644 KB Output is correct
33 Correct 56 ms 3648 KB Output is correct
34 Correct 84 ms 3652 KB Output is correct
35 Correct 55 ms 3708 KB Output is correct
36 Correct 82 ms 3716 KB Output is correct
37 Correct 40 ms 3708 KB Output is correct