답안 #228979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
228979 2020-05-03T08:14:57 Z alishahali1382 Palembang Bridges (APIO15_bridge) C++14
100 / 100
84 ms 9568 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, LOG=20;

ll n, m, k, u, v, x, y, t, l, r, ans, constant;
pll A[MAXN];
ll B[MAXN], C[MAXN];

struct Solver{
	priority_queue<ll> pref;
	priority_queue<ll, vector<ll>, greater<ll>> suff;
	ll pref_sum=0, suff_sum=0, mid=inf;
	
	void Add(ll x){
		if (x<=mid) pref.push(x), pref_sum+=x;
		else suff.push(x), suff_sum+=x;
		while (pref.size()<suff.size()){
			ll x=suff.top();
			suff.pop();
			suff_sum-=x;
			
			pref.push(x);
			pref_sum+=x;
			//cerr<<"move2 "<<x<<endl;
		}
		while (pref.size()-suff.size()>1){
			ll x=pref.top();
			pref.pop();
			pref_sum-=x;
			
			suff.push(x);
			suff_sum+=x;
			//cerr<<"move1 "<<x<<endl;
		}
		mid=pref.top();
	}
	ll Get(){
		return suff_sum-suff.size()*mid + pref.size()*mid-pref_sum;
	}
	
} S1, S2;

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>k>>m;
	//assert(m<=1000 && k==1);
	char c1, c2;
	while (m--){
		cin>>c1>>l>>c2>>r;
		if (l>r) swap(l, r);
		if (c1!=c2){
			A[++n]={l, r};
			constant++;
		}
		else constant+=r-l;
	}
	
	sort(A+1, A+n+1, [](pll i, pll j){
		return i.first+i.second<j.first+j.second;
	});
	
	for (int i=1; i<=n; i++){
		S1.Add(A[i].first);
		S1.Add(A[i].second);
		B[i]=S1.Get();
	}
	for (int i=n; i; i--){
		S2.Add(A[i].first);
		S2.Add(A[i].second);
		C[i]=S2.Get();
	}
	ans=B[n];
	if (k==2){
		for (int i=1; i<n; i++) ans=min(ans, B[i]+C[i+1]);
	}
	//debug(ans)
	cout<<ans+constant<<'\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

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 660 KB Output is correct
5 Correct 6 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 308 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 42 ms 7908 KB Output is correct
13 Correct 82 ms 9568 KB Output is correct
14 Correct 64 ms 7984 KB Output is correct
15 Correct 55 ms 5788 KB Output is correct
16 Correct 49 ms 8804 KB Output is correct
17 Correct 70 ms 9444 KB Output is correct
18 Correct 48 ms 9528 KB Output is correct
19 Correct 74 ms 9432 KB Output is correct
20 Correct 55 ms 9060 KB Output is correct
21 Correct 73 ms 9188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 5 ms 360 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 360 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 400 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 436 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 436 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 436 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 436 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 6 ms 512 KB Output is correct
23 Correct 6 ms 384 KB Output is correct
24 Correct 6 ms 384 KB Output is correct
25 Correct 41 ms 7908 KB Output is correct
26 Correct 56 ms 8148 KB Output is correct
27 Correct 74 ms 8928 KB Output is correct
28 Correct 84 ms 9508 KB Output is correct
29 Correct 81 ms 9516 KB Output is correct
30 Correct 41 ms 5376 KB Output is correct
31 Correct 42 ms 8804 KB Output is correct
32 Correct 70 ms 9444 KB Output is correct
33 Correct 46 ms 9552 KB Output is correct
34 Correct 79 ms 9496 KB Output is correct
35 Correct 53 ms 9068 KB Output is correct
36 Correct 73 ms 9260 KB Output is correct
37 Correct 47 ms 7932 KB Output is correct