Submission #332428

# Submission time Handle Problem Language Result Execution time Memory
332428 2020-12-02T13:19:05 Z zaneyu Palembang Bridges (APIO15_bridge) C++14
100 / 100
91 ms 6520 KB
/*input
1 5
B0A4
B1B3
A5B7
B2A6
B1A7
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = long long;
using ld = long double;
using pii = pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define MP make_pair
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    } 
    return res;
}
const ll maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
#define int ll
ll pv[maxn],suff[maxn];
bool cmp(pii a,pii b){
    return a.f+a.s<b.f+b.s;
}
int32_t main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int k,n;
    cin>>k>>n;
    ll ans=0;
    vector<pii> v;
    REP(i,n){
        char a,b;
        int x,y;
        cin>>a>>x>>b>>y;
        if(a==b) ans+=abs(x-y);
        else v.pb({x,y}),ans++;
    }
    sort(ALL(v),cmp);
    n=sz(v);
    if(!sz(v)){
        cout<<ans;
        return 0;
    }
    priority_queue<int> mn,mx;
    ll mns=0,mxs=0;
    REP(i,n){
        if(v[i].f>v[i].s) swap(v[i].f,v[i].s);
        if(!sz(mx)){
            mn.push(v[i].f),mns+=v[i].f;
            mx.push(-v[i].s),mxs+=v[i].s;
            pv[i]=mxs-mns;
            continue;
        }
        int mnn=mn.top(),mxx=-mx.top();
        if(v[i].s<=mnn){
            mn.pop();
            mns-=mnn;
            mn.push(v[i].f),mns+=v[i].f;
            mn.push(v[i].s),mns+=v[i].s;
            mx.push(-mnn),mxs+=mnn;
        }
        else if(v[i].f>=mxx){
            mx.pop();
            mxs-=mxx;
            mx.push(-v[i].f),mxs+=v[i].f;
            mx.push(-v[i].s),mxs+=v[i].s;
            mn.push(mxx),mns+=mxx;
        }
        else{
            mx.push(-v[i].s),mxs+=v[i].s;
            mn.push(v[i].f),mns+=v[i].f;
        }
        assert(sz(mx)==sz(mn));
        pv[i]=mxs-mns;
    }
    while(sz(mn)) mn.pop();
    while(sz(mx)) mx.pop();
    mns=mxs=0;
    ll cur=pv[n-1];
    for(int i=n-1;i>=0;i--){
        if(!sz(mx)){
            mn.push(v[i].f),mns+=v[i].f;
            mx.push(-v[i].s),mxs+=v[i].s;
            suff[i]=mxs-mns;
            continue;
        }
        int mnn=mn.top(),mxx=-mx.top();
        if(v[i].s<=mnn){
            mn.pop();
            mns-=mnn;
            mn.push(v[i].f),mns+=v[i].f;
            mn.push(v[i].s),mns+=v[i].s;
            mx.push(-mnn),mxs+=mnn;
        }
        else if(v[i].f>=mxx){
            mx.pop();
            mxs-=mxx;
            mx.push(-v[i].f),mxs+=v[i].f;
            mx.push(-v[i].s),mxs+=v[i].s;
            mn.push(mxx),mns+=mxx;
        }
        else{
            mx.push(-v[i].s),mxs+=v[i].s;
            mn.push(v[i].f),mns+=v[i].f;
        }
        assert(sz(mx)==sz(mn));
        suff[i]=mxs-mns;
    }
    if(k==2) REP(i,n-1) MNTO(cur,pv[i]+suff[i+1]);
    cout<<ans+cur;
    
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 46 ms 6116 KB Output is correct
13 Correct 91 ms 6372 KB Output is correct
14 Correct 76 ms 5988 KB Output is correct
15 Correct 55 ms 3560 KB Output is correct
16 Correct 44 ms 6372 KB Output is correct
17 Correct 77 ms 6372 KB Output is correct
18 Correct 55 ms 6372 KB Output is correct
19 Correct 83 ms 6372 KB Output is correct
20 Correct 56 ms 6372 KB Output is correct
21 Correct 80 ms 6372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 45 ms 6116 KB Output is correct
26 Correct 66 ms 6372 KB Output is correct
27 Correct 88 ms 6372 KB Output is correct
28 Correct 91 ms 6372 KB Output is correct
29 Correct 90 ms 6392 KB Output is correct
30 Correct 47 ms 3304 KB Output is correct
31 Correct 44 ms 6372 KB Output is correct
32 Correct 78 ms 6500 KB Output is correct
33 Correct 59 ms 6372 KB Output is correct
34 Correct 83 ms 6372 KB Output is correct
35 Correct 58 ms 6520 KB Output is correct
36 Correct 81 ms 6372 KB Output is correct
37 Correct 40 ms 6372 KB Output is correct