Submission #553156

#TimeUsernameProblemLanguageResultExecution timeMemory
553156uroskPalembang Bridges (APIO15_bridge)C++14
100 / 100
276 ms15168 KiB
// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) ((ll)(a.size()))
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"=============================================================================\n"
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;

template<typename T> using vc = vector<T>;
template<typename T> using vvc = vc<vc<T>>;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
    return uniform_int_distribution<ll>(l,r)(rng);
}

void setIO(string inoutname)
{
	freopen((inoutname+".in").c_str(),"r",stdin);
    	freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
   if(b==0) return a;
   if(a==0) return b;
   if(a>=b)  return gcd(a%b,b);
   return  gcd(a,b%a);
}
ll lcm(ll a,ll b){
   return (a/gcd(a,b))*b;
}
ll add(ll x,ll y){
    x+=y;
    if(x<0){
        x%=mod;
        x+=mod;
	if(x==mod) x = 0;
    }else{
        if(x>=mod) x%=mod;
    }
    return x;
}
ll mul(ll a,ll b){
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}
ll po(ll x,ll y){
    if(y==0) return 1LL;
    ll ans = po(x,y/2);
    ans = mul(ans,ans);
    if(y&1) ans = mul(ans,x);
    return ans;
}
ll inv(ll x){return po(x,mod-2);}
#define maxn 100005
ll n,k;
ll a[maxn];
ll dp[maxn][2];
bool cmp(pll x,pll y){return x.fi+x.sc<y.fi+y.sc;}
void tc(){
	daj_mi_malo_vremena
    cin >> k >> n;
    ll sum = 0;
    vector<pll> v;
    for(ll i = 1;i<=n;i++){
        ll x,y; string sx,sy;
        cin >> sx >> x >> sy >> y;
        if(sx==sy) sum+=abs(x-y);
        else v.pb({min(x,y),max(x,y)});
    }
    if(sz(v)==0){
        cout<<sum<<endl;
        return;
    }
    sort(all(v),cmp);
    for(ll j = 0;j<=1;j++){
        multiset<ll> s;
        s.insert(v[0].fi);
        s.insert(v[0].sc);
        dp[0][j] = v[0].sc-v[0].fi;
        auto it = s.begin();
        for(ll i = 1;i<sz(v);i++){
            ll l = v[i].fi;
            ll r = v[i].sc;
            s.insert(l);
            s.insert(r);
            ll last = *it;
            dp[i][j] = dp[i-1][j];
            if(r>=*it&&l>=*it) it++;
            if(r<*it&&l<*it){
                it--;
                dp[i][j] += 2*abs(*it-last);
            }
            dp[i][j] += abs(l-*it) + abs(r-*it);
        }
        reverse(all(v));
    }
    if(k==1){
        cout<<dp[sz(v)-1][0] + sz(v) + sum<<endl;
        return;
    }
    ll ans = min(dp[sz(v)-1][0],dp[sz(v)-1][1]);
    for(ll i = 0;i<sz(v);i++){
        ans = min(ans,dp[i][0] + dp[sz(v)-2-i][1]);
    }
    ans+=sz(v)+sum;
    cout<<ans<<endl;
}
int main(){
	daj_mi_malo_vremena
	//setIO("lol");
	int t; t = 1;
	while(t--){
		tc();
	}
	return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
out: 24
*/
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
out: 22
*/

Compilation message (stderr)

bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:50:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |  freopen((inoutname+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:51:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |      freopen((inoutname+".out").c_str(),"w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...