Submission #1101025

# Submission time Handle Problem Language Result Execution time Memory
1101025 2024-10-15T10:25:49 Z coolsentenceidontremember Palembang Bridges (APIO15_bridge) C++17
100 / 100
72 ms 5716 KB



#include"bits/stdc++.h"
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define mp make_pair
#define make_rng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define make_rng64 mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;

void setIO(string s = ""){
 ios_base::sync_with_stdio(false);
 cin.tie(nullptr);
 cout.tie(nullptr);
 if (!s.empty()){
	freopen((s+".in").c_str(), "r", stdin);
	freopen((s+".out").c_str(), "w", stdout);
	}
}

const double PI = acos(-1);

struct chash{
	const uint64_t C = uint64_t(2e18 * PI) + 71;
	const uint32_t random = chrono::steady_clock::now().time_since_epoch().count();
	size_t operator()(uint64_t x) const {
		return __builtin_bswap64((x^random)*C);
	}
};

const int N = 1e5 + 1;



bool cmp(const pii &a, const pii &b){
	return a.ff + a.ss < b.ff + b.ss;
}

ll pfx[N];

priority_queue<int> lpq, rpq;

ll lsum, rsum;

void insert(int x){
	int med = (lpq.size() ? lpq.top() : INT_MAX);
	if (x <= med){
		lpq.push(x);
		lsum += x;
	} else {
		rpq.push(-x);
		rsum += x;
	}
	
	if (lpq.size() < rpq.size()){
		int a = -rpq.top();
		rpq.pop();
		lpq.push(a);
		lsum += a; rsum -= a;
	} else if (lpq.size() - rpq.size() > 1){
		int a = lpq.top();
		lpq.pop();
		rpq.push(-a);
		lsum -= a; rsum += a;
	}
	
}



int main(){
	setIO();
	int k, n;
	cin >> k >> n;
	ll same = 0;
	vector<pair<int, int>> v;
	for (int i = 1; i <= n; i++){
		char s1, s2;
		int s, t;
		cin >> s1 >> s >> s2 >> t;
		if (s1 == s2) same += abs(s-t);
		else v.push_back(mp(s, t));
	}
	v.push_back({0, 0});
	sort(v.begin(), v.end(), cmp);
    n = v.size() - 1;
    pfx[0] = 0;
    lsum = rsum = 0;
    for (int i = 1;i <= n; i++){
    	insert(v[i].ff);
    	insert(v[i].ss);
    	pfx[i] = rsum - lsum;
	}
	ll ans = pfx[n];
	if (k == 2){
		while (lpq.size()) lpq.pop();
		while (rpq.size()) rpq.pop();
		lsum = rsum = 0;
		for (int i = n; i; i--){
			insert(v[i].ff);
			insert(v[i].ss);
			ans = min(ans, rsum - lsum + pfx[i-1]);
		}
	}
	if (v.empty()) ans = 0;
	cout << ans + same + v.size() - 1 << '\n';
	
}

Compilation message

bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:21:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  freopen((s+".in").c_str(), "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:22:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |  freopen((s+".out").c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 21 ms 4044 KB Output is correct
13 Correct 42 ms 5716 KB Output is correct
14 Correct 32 ms 4300 KB Output is correct
15 Correct 24 ms 3540 KB Output is correct
16 Correct 24 ms 5060 KB Output is correct
17 Correct 41 ms 5572 KB Output is correct
18 Correct 28 ms 5060 KB Output is correct
19 Correct 39 ms 5572 KB Output is correct
20 Correct 28 ms 5316 KB Output is correct
21 Correct 37 ms 5316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 472 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 480 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 504 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 472 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 504 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 27 ms 4036 KB Output is correct
26 Correct 57 ms 4300 KB Output is correct
27 Correct 68 ms 5212 KB Output is correct
28 Correct 72 ms 5632 KB Output is correct
29 Correct 69 ms 5572 KB Output is correct
30 Correct 35 ms 3024 KB Output is correct
31 Correct 32 ms 4940 KB Output is correct
32 Correct 62 ms 5576 KB Output is correct
33 Correct 52 ms 5580 KB Output is correct
34 Correct 53 ms 5572 KB Output is correct
35 Correct 38 ms 5324 KB Output is correct
36 Correct 54 ms 5316 KB Output is correct
37 Correct 23 ms 4044 KB Output is correct