Submission #366143

# Submission time Handle Problem Language Result Execution time Memory
366143 2021-02-13T10:13:20 Z YJU Palembang Bridges (APIO15_bridge) C++14
100 / 100
117 ms 8952 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

priority_queue<ll> pqa;
priority_queue<ll,vector<ll>,greater<ll> > pqb;

ll k,n,sum,x,y,ans,suf[N],pre[N];
char A,B;
vector<pll> v;


void add(ll d){
	if(SZ(pqa)&&d<pqa.top())sum-=d,pqa.push(d);
	else sum+=d,pqb.push(d);
	while(SZ(pqa)>SZ(pqb)){
		d=pqa.top();pqa.pop();pqb.push(d);
		sum+=d*2;
	}
	while(SZ(pqb)>SZ(pqa)+1){
		d=pqb.top();pqb.pop();pqa.push(d);
		sum-=d*2;
	}
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>k>>n;
	REP1(i,n){
		cin>>A>>x>>B>>y;
		if(A==B){ans+=abs(x-y);continue;}
		++ans;
		v.pb(mp(x+y,y));
	}
	sort(v.begin(),v.end());
	n=SZ(v);
	if(n==0){cout<<ans<<"\n";return 0;}
	while(SZ(pqa))pqa.pop();
	while(SZ(pqb))pqb.pop();
	sum=0;
	REP(i,n){
		add(v[i].X-v[i].Y);
		add(v[i].Y);
		//cout<<sum<<" "<<pqb.top()<<" "<<SZ(pqa)-SZ(pqb)<<"\n";
		pre[i]=sum;
	}
	while(SZ(pqa))pqa.pop();
	while(SZ(pqb))pqb.pop();
	sum=0;
	for(int i=n-1;i>=0;i--){
		add(v[i].X-v[i].Y);
		add(v[i].Y);
		suf[i]=sum;
	}
	//cout<<pre[n-1]<<"\n";
	ll tans=ans+pre[n-1];
	if(k==2){
		for(int i=0;i+1<n;i++)tans=min(tans,ans+pre[i]+suf[i+1]);
	}
	cout<<tans<<"\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
*/
# 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 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 2 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 56 ms 7268 KB Output is correct
13 Correct 108 ms 8804 KB Output is correct
14 Correct 85 ms 7192 KB Output is correct
15 Correct 61 ms 5352 KB Output is correct
16 Correct 56 ms 8036 KB Output is correct
17 Correct 73 ms 8804 KB Output is correct
18 Correct 87 ms 8420 KB Output is correct
19 Correct 80 ms 8804 KB Output is correct
20 Correct 55 ms 8292 KB Output is correct
21 Correct 80 ms 8548 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 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 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 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 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 384 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 492 KB Output is correct
16 Correct 1 ms 384 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 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 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 2 ms 492 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 2 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 46 ms 7268 KB Output is correct
26 Correct 74 ms 7396 KB Output is correct
27 Correct 97 ms 8164 KB Output is correct
28 Correct 101 ms 8804 KB Output is correct
29 Correct 117 ms 8952 KB Output is correct
30 Correct 53 ms 4456 KB Output is correct
31 Correct 57 ms 8164 KB Output is correct
32 Correct 74 ms 8804 KB Output is correct
33 Correct 84 ms 8420 KB Output is correct
34 Correct 83 ms 8828 KB Output is correct
35 Correct 56 ms 8292 KB Output is correct
36 Correct 79 ms 8548 KB Output is correct
37 Correct 41 ms 7300 KB Output is correct