Submission #332849

# Submission time Handle Problem Language Result Execution time Memory
332849 2020-12-03T17:32:37 Z errorgorn Palembang Bridges (APIO15_bridge) C++17
100 / 100
204 ms 7828 KB
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n,k;
vector<ii> v;

ll val[100005];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	cin>>k>>n;
	
	char ca,cb;
	ll qa,qb;
	ll same=0;
	
	rep(x,0,n){
		cin>>ca>>qa>>cb>>qb;
		
		same+=abs(qa-qb);
		if (ca!=cb) v.push_back(ii(min(qa,qb),max(qa,qb))),same++;
	}
	
	sort(all(v),[](ii i,ii j){
		return i.fi+i.se<j.fi+j.se;
	});
	
	if (k==1){
		ll delta=-sz(v);
		ll curr=0;
		priority_queue<ll,vector<ll>,greater<ll> > pq;
		
		for (auto &it:v){
			//cout<<it.fi<<" "<<it.se<<endl;
			pq.push(it.fi);
			pq.push(it.se);
			curr+=it.fi;
		}
		
		ll ans=curr;
		
		ll pos=0;
		while (!pq.empty()){
			ll temp=pq.top();
			pq.pop();
			
			curr+=delta*(temp-pos);
			pos=temp;
			delta++;
			
			ans=min(ans,curr);
			//cout<<curr<<endl;
		}
		
		cout<<ans*2+same<<endl;
	}
	else{
		ll delta,curr,pos;
		
		delta=0;
		curr=0;
		
		pos=0;
		
		priority_queue<ll,vector<ll>,greater<ll> > pq;
		rep(x,0,sz(v)){
			if (pos<v[x].fi){
				delta--;
				pq.push(v[x].fi);
				curr+=v[x].fi-pos;
			}
			pq.push(v[x].se);
			
			if (delta<0){
				ll temp=pq.top();
				pq.pop();
				
				curr+=delta*(temp-pos);
				pos=temp;
				delta++;
			}
			val[x+1]+=curr;
		}
		
		//rep(x,0,sz(v)+1) cout<<val[x]<<" "; cout<<endl;
		
		delta=0;
		curr=0;
		
		pos=1e9+100;
		
		priority_queue<ll> pq2;
		rep(x,sz(v),0){
			if (v[x].se<pos){
				delta--;
				pq2.push(v[x].se);
				curr+=pos-v[x].se;
			}
			pq2.push(v[x].fi);
			
			if (delta<0){
				ll temp=pq2.top();
				pq2.pop();
				
				curr+=delta*(pos-temp);
				pos=temp;
				delta++;
			}
			
			val[x]+=curr;
		}
		
		//rep(x,0,sz(v)+1) cout<<val[x]<<" "; cout<<endl;
		
		ll ans=1e18;
		rep(x,0,sz(v)+1) ans=min(ans,val[x]);
		cout<<ans*2+same<<endl;
	}
	
}
# 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 384 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 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 384 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 2 ms 364 KB Output is correct
12 Correct 34 ms 4964 KB Output is correct
13 Correct 67 ms 4964 KB Output is correct
14 Correct 55 ms 4836 KB Output is correct
15 Correct 39 ms 2792 KB Output is correct
16 Correct 40 ms 4964 KB Output is correct
17 Correct 48 ms 4964 KB Output is correct
18 Correct 51 ms 4964 KB Output is correct
19 Correct 55 ms 4964 KB Output is correct
20 Correct 43 ms 4984 KB Output is correct
21 Correct 52 ms 4964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 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
# Verdict Execution time Memory Grader output
1 Correct 0 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 384 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 50 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 204 ms 6116 KB Output is correct
26 Correct 48 ms 6392 KB Output is correct
27 Correct 56 ms 7012 KB Output is correct
28 Correct 62 ms 7652 KB Output is correct
29 Correct 66 ms 7828 KB Output is correct
30 Correct 32 ms 4200 KB Output is correct
31 Correct 34 ms 7012 KB Output is correct
32 Correct 50 ms 7652 KB Output is correct
33 Correct 36 ms 7268 KB Output is correct
34 Correct 60 ms 7652 KB Output is correct
35 Correct 41 ms 7140 KB Output is correct
36 Correct 54 ms 7396 KB Output is correct
37 Correct 25 ms 6116 KB Output is correct