Submission #1012477

# Submission time Handle Problem Language Result Execution time Memory
1012477 2024-07-02T08:25:16 Z akkshaysr Palembang Bridges (APIO15_bridge) C++17
100 / 100
228 ms 17864 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fr first
#define se second
#define rep(i,a,b) for(int i = a; i < (b); ++i)
#define rrep(i,a,b) for(int i = a; i > (b); --i)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define IN(i,l,r) (l<i&&i<r)
#define pb push_back
#define ones __builtin_popcountll
using namespace std;
using namespace __gnu_pbds;
template <class T>
using OSTree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef long long ll;
int k,n;
vi home,office,ord;

void fill_cost(vll &sol){
	OSTree<pi> iset;
	sol[0] = abs(office[ord[0]] - home[ord[0]]);
	iset.insert({office[ord[0]],0});
	iset.insert({home[ord[0]],1});
	auto med = [&]() -> int {
		return iset.find_by_order((sz(iset)-1)/2)->fr;
	};
	int c = 2;
	rep(i,1,n){
		int old = med();
		iset.insert({home[ord[i]],c++});
		int neo = med();
		ll del = abs(home[ord[i]] - old) + (old - neo);
		old = neo;
		iset.insert({office[ord[i]],c++});
		neo = med();
		del += abs(office[ord[i]] - old);
		sol[i] = sol[i-1] + del;
	}
}

int main(){
	cin.tie(0)->sync_with_stdio(false);
	cin >> k >> n;
	ll no_cross = 0;
	rep(i,0,n){
		char p,q; int s,t; cin >> p >> s >> q >> t;
		if(p == q){
			no_cross += abs(t - s);
		}else{
			home.pb(s);
			office.pb(t);
		}
	}
	n = sz(home);
	if(n == 0){
		cout << no_cross << '\n'; return 0;
	}
	ord = vi(n,0);
	iota(all(ord),0);
	sort(all(ord),[](int a,int b){return home[a] + office[a] < home[b] + office[b];});
	vll pre(n,0), suf(n,0);
	fill_cost(pre);
	ll cross = pre[n-1];
	if(k == 1){
		cout << (no_cross + cross + n) << '\n'; return 0;
	}
	reverse(all(ord));
	fill_cost(suf);
	rep(i,1,n) cross = min(cross, pre[i-1] + suf[n-1-i]);
	cout << (no_cross + cross + n) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 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 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 142 ms 16344 KB Output is correct
13 Correct 138 ms 17860 KB Output is correct
14 Correct 113 ms 15136 KB Output is correct
15 Correct 68 ms 10700 KB Output is correct
16 Correct 161 ms 17196 KB Output is correct
17 Correct 123 ms 17708 KB Output is correct
18 Correct 70 ms 17344 KB Output is correct
19 Correct 118 ms 17768 KB Output is correct
20 Correct 174 ms 17352 KB Output is correct
21 Correct 121 ms 17604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 183 ms 16464 KB Output is correct
26 Correct 198 ms 16580 KB Output is correct
27 Correct 217 ms 17296 KB Output is correct
28 Correct 228 ms 17864 KB Output is correct
29 Correct 217 ms 17856 KB Output is correct
30 Correct 100 ms 9424 KB Output is correct
31 Correct 182 ms 17192 KB Output is correct
32 Correct 168 ms 17848 KB Output is correct
33 Correct 112 ms 17476 KB Output is correct
34 Correct 156 ms 17612 KB Output is correct
35 Correct 227 ms 17348 KB Output is correct
36 Correct 170 ms 17572 KB Output is correct
37 Correct 210 ms 16420 KB Output is correct