답안 #317773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317773 2020-10-30T11:08:49 Z Return_0 Palembang Bridges (APIO15_bridge) C++17
100 / 100
97 ms 10472 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("O2,unroll-loops")
// #pragma GCC target ("avx,avx2,fma")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef LOCAL
#define dbg(x) cout<< #x << " : " << x << endl
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1);
#else
#define dbg(x) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< x << endl, 0
#define cl const ll
#define fr first
#define sc second
#define mid ((l + r) / 2)
#define All(x) (x).begin(), (x).end()

const long long inf = 1e18;
cl mod = 1e9 + 7, MOD = 998244353;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
    return out << '(' << a.first << ", " << a.second << ')';    }
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
    if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

bool cmp (const pll &x, const pll &y) { return (x.fr + x.sc) < (y.fr + y.sc);   }

cl N = 1e5 + 7;

long long ans;

char c1 [N], c2 [N];
ll A [N], B [N], n;
vector <pll> vec;
vector <ll> pref, suff;
priority_queue <ll> lower, upper;

ll Insert (ll val) {
	ll X = lower.top();
	ll Y = -upper.top();

	ll res = 0;
	if (val > Y){
		upper.push(-val);
		res += val - X;
		if (SZ(upper) > SZ(lower)) {
			res -= X * (SZ(lower) - SZ(upper));
			upper.pop();
			lower.push(Y);
			res -= 2 * Y;
			res += Y * (SZ(lower) - SZ(upper));
		}
	}

	else if (val >= X){
		if (SZ(lower) > SZ(upper)) {
			res += val - X;
			upper.push(-val);
		}
	
    	else {
			lower.push(val);
			res += X - val;
			res -= X * (SZ(lower) - SZ(upper));
			res += val * (SZ(lower) - SZ(upper));
		}
	}

	else {
		lower.push(val);
		res += X - val;
		if (SZ(lower) > SZ(upper) + 1) {
			lower.pop();
			upper.push(-X);
		}
	}

	return res;
}

vector <ll> Fill () {
    vector <ll> ret;
	
    while (SZ(lower)) lower.pop();
	while (SZ(upper)) upper.pop();
	
    lower.push(vec[0].fr);
	upper.push(-vec[0].sc);
	ret.push_back(vec[0].sc - vec[0].fr);
	
    for (ll i = 1; i < n; i++) {
		ret.push_back(ret.back() + Insert(vec[i].fr) + Insert(vec[i].sc));
	}

	return ret;
}

int main ()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll k, i, j, x, y, sum = 0;

    cin>> k >> n;
    for (i = 0; i < n; i++) {
        cin>> c1[i] >> A[i] >> c2[i] >> B[i];
        if (A[i] > B[i])    swap(A[i], B[i]);
        if (c1[i] != c2[i]) vec.push_back({A[i], B[i]}), sum++;
        else sum += abs(A[i] - B[i]);
    }
    
    sort(All(vec), cmp);
    n = SZ(vec);
    if (!n) kill(sum);

    pref = Fill();
    reverse(All(vec));

    suff = Fill();
    reverse(All(suff));

    if (k == 1) kill(pref.back() + sum);

    ans = pref.back();
    reverse(All(vec));
    
    for (i = 0; i < n-1; i++) {
    	ans = min(ans, pref[i] + suff[i+1]);
    }

    cout << ans + sum << endl;

    // cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

    return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

*/

Compilation message

bridge.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
bridge.cpp:40:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   40 |     if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |     ^~
bridge.cpp:40:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   40 |     if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |                                    ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:119:14: warning: unused variable 'j' [-Wunused-variable]
  119 |     ll k, i, j, x, y, sum = 0;
      |              ^
bridge.cpp:119:17: warning: unused variable 'x' [-Wunused-variable]
  119 |     ll k, i, j, x, y, sum = 0;
      |                 ^
bridge.cpp:119:20: warning: unused variable 'y' [-Wunused-variable]
  119 |     ll k, i, j, x, y, sum = 0;
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 44 ms 8808 KB Output is correct
13 Correct 97 ms 10472 KB Output is correct
14 Correct 82 ms 8812 KB Output is correct
15 Correct 57 ms 6124 KB Output is correct
16 Correct 46 ms 9708 KB Output is correct
17 Correct 81 ms 10396 KB Output is correct
18 Correct 61 ms 10088 KB Output is correct
19 Correct 87 ms 10472 KB Output is correct
20 Correct 55 ms 9960 KB Output is correct
21 Correct 83 ms 10196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 43 ms 8936 KB Output is correct
26 Correct 70 ms 9064 KB Output is correct
27 Correct 92 ms 9832 KB Output is correct
28 Correct 95 ms 10344 KB Output is correct
29 Correct 97 ms 10344 KB Output is correct
30 Correct 51 ms 5612 KB Output is correct
31 Correct 54 ms 9832 KB Output is correct
32 Correct 82 ms 10344 KB Output is correct
33 Correct 60 ms 10088 KB Output is correct
34 Correct 85 ms 10344 KB Output is correct
35 Correct 56 ms 9960 KB Output is correct
36 Correct 84 ms 10212 KB Output is correct
37 Correct 37 ms 8936 KB Output is correct