답안 #537283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537283 2022-03-14T21:41:10 Z crystaalroo Palembang Bridges (APIO15_bridge) C++17
100 / 100
70 ms 6608 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
using namespace std;
 
long long gcd(long long a, long long b){ return b? gcd(b, a%b) : a; }
long long lcm(long long a, long long b){ return (!a or !b)? 0 : a * b / gcd(a,b); } 
long long  poww(long long  a, long long b){
	long long res = 1;
	while(b){ 
		if(b%2) res = res * a ; 
		a = a * a;  b>>=1;
	} return res;
}
 
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}
 
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template<class t,class u>bool mmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool mmin(t&a,u b){if(b<a)a=b;return b<a;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using li = long long;
using vi = vc<int>;
using ii = pair<int,int>;
//  ('-')7


int main(){_
 
	auto solve=[&](){
		int n,k; cin>>k>>n;
		li sum = 0;
		vector<ii>v;
		fore(i,0,n){
			char a,b; int c,d; cin>>a>>c>>b>>d;
			if(a==b)sum+=abs(c-d);
			else{
				sum+=1;
				v.pb({c,d});
			}
		}
		if(sz(v)==0){
			cout<<sum<<ENDL;
			return;
		}
		n = sz(v);
		sort(all(v),[&](ii a, ii b){return a.f+a.s<b.f+b.s;});
		priority_queue<int>l;
		priority_queue<int,vi,greater<int>>r;
		li ls = 0, rs = 0;
		auto f=[&](int u){
			int mid = sz(l)?l.top():inf<int>;
			if(u<=mid){
				l.push(u);
				ls+=u;
			}
			else{
				r.push(u);
				rs+=u;
			}
			if(sz(r)+1<sz(l)){
				int nx = l.top(); l.pop();
				r.push(nx);
				ls-=nx;
				rs+=nx;
			}
			else if(sz(r)>sz(l)){
				int nx = r.top(); r.pop();
				l.push(nx);
				rs-=nx; ls+=nx;
			}
		};
		vc<li>pref(n,0);
		vc<li>suf(n,0);
		fore(i,0,n){
			f(v[i].f);
			f(v[i].s);
			pref[i]=rs-ls;
		}
		rs = 0, ls = 0;
		r=priority_queue<int,vi,greater<int>>();
		l = priority_queue<int>();
		for(int i = n-1;i>=0;i--){
			f(v[i].f);
			f(v[i].s);
			suf[i]=rs-ls;
		}
		li sol = pref[n-1];
		if(k==2)fore(i,0,n-1)sol = min(sol,pref[i]+suf[i+1]);
		cout<<sol+sum<<ENDL;
	};
	//int t; cin>>t; while(t--)
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 34 ms 5060 KB Output is correct
13 Correct 70 ms 6432 KB Output is correct
14 Correct 63 ms 5048 KB Output is correct
15 Correct 47 ms 3996 KB Output is correct
16 Correct 51 ms 5704 KB Output is correct
17 Correct 56 ms 6276 KB Output is correct
18 Correct 55 ms 6252 KB Output is correct
19 Correct 61 ms 6284 KB Output is correct
20 Correct 53 ms 6192 KB Output is correct
21 Correct 57 ms 6000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 352 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 36 ms 4680 KB Output is correct
26 Correct 60 ms 5100 KB Output is correct
27 Correct 66 ms 5844 KB Output is correct
28 Correct 69 ms 6608 KB Output is correct
29 Correct 68 ms 6192 KB Output is correct
30 Correct 36 ms 3488 KB Output is correct
31 Correct 44 ms 5824 KB Output is correct
32 Correct 49 ms 6184 KB Output is correct
33 Correct 53 ms 6200 KB Output is correct
34 Correct 59 ms 6236 KB Output is correct
35 Correct 44 ms 6180 KB Output is correct
36 Correct 57 ms 5996 KB Output is correct
37 Correct 26 ms 4684 KB Output is correct