Submission #317773

#TimeUsernameProblemLanguageResultExecution timeMemory
317773Return_0Palembang Bridges (APIO15_bridge)C++17
100 / 100
97 ms10472 KiB
#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 (stderr)

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;
      |                    ^
#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...