Submission #357721

#TimeUsernameProblemLanguageResultExecution timeMemory
357721AriaHPalembang Bridges (APIO15_bridge)C++11
100 / 100
105 ms6116 KiB
/** Im the best because i work as hard as i possibly can **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
#define SZ(x) 			    (int)x.size()

const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

int n, k;

ll base, tot, pre[N], suf[N], L, R;

vector < pii > vec;

priority_queue < int > lq;
priority_queue < int, vector < int > , greater < int > > rq;

bool cmp(pii a, pii b)
{
    return (a.F + a.S) < (b.F + b.S);
}

void add(int x)
{
    if(lq.empty() || x <= lq.top())
    {
	lq.push(x);
	L += x;
    }
    else
    {
	rq.push(x);
	R += x;
    }
    if(SZ(lq) - 1 > SZ(rq))
    {
	int z = lq.top();
	lq.pop();
	rq.push(z);
	L -= z, R += z;
    }
    if(SZ(lq) < SZ(rq))
    {
	int z = rq.top();
	rq.pop();
	lq.push(z);
	L += z, R -= z;
    }
}

int main()
{
    fast_io;
    cin >> k >> n;
    for(int i = 1; i <= n; i ++)
    {
	string a, b;
	int x, y;
	cin >> a >> x >> b >> y;
	base += (a == b? abs(x - y) : 1);
	if(a != b)
	{
	    vec.push_back(Mp(min(x, y), max(x, y)));
	}
    }
    sort(all(vec), cmp);
    for(int i = 0; i < SZ(vec); i ++)
    {
	add(vec[i].F);
	add(vec[i].S);
	pre[i + 1] = R - L;
	///cout << vec[i].F << " " << vec[i].S << " " << L << " " << R << endl;
    }
    tot = pre[SZ(vec)];
    if(k ^ 1)
    {
	while(SZ(lq)) lq.pop();
	while(SZ(rq)) rq.pop();
	L = 0;
	R = 0;
	for(int i = SZ(vec) - 1; ~i; i --)
	{
	    add(vec[i].F);
	    add(vec[i].S);
	    tot = min(tot, R - L + pre[i]);
	}
    }
    cout << tot + base;
    return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
#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...