답안 #1079162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079162 2024-08-28T11:26:09 Z Muhammet Palembang Bridges (APIO15_bridge) C++17
100 / 100
235 ms 20292 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define sz(x) (int)x.size()
#define ff first
#define ss second
 
const int N = 300005;
const int M = 1e9 + 7;
 
int T, n, k, s1[N], s2[N];
 
char c1[N], c2[N];
 
signed main(){
	ios::sync_with_stdio(false); cin.tie(0);
 
	cin >> k >> n;
	for(int i = 1; i <= n; i++){
		cin >> c1[i] >> s1[i] >> c2[i] >> s2[i];
	}
 

    int ans = 0;
	if(k == 1){
		vector <int> v;
		for(int i = 1; i <= n; i++){
			if(c1[i] == c2[i]){
				ans += abs(s1[i]-s2[i]);
			}
			else {
				v.push_back(s1[i]);
				v.push_back(s2[i]);
			}
		}
		sort(v.begin(), v.end());
		int x = (sz(v)+1)/2;
		x--;
		if(x >= 0) x = v[x];
		for(auto i : v){
			ans += abs(i-x);
		}
		cout << ans+sz(v)/2 << "\n";
        return 0;
	}

	vector <pair<int,pair<int,int>>> v1;

	for(int i = 1; i <= n; i++){
		if(c1[i] != c2[i]){
			v1.push_back({(s1[i]+s2[i])/2,{min(s1[i],s2[i]),max(s1[i],s2[i])}});
		}
        else {
            ans += abs(s1[i]-s2[i]);
        }
	}
    
    ans += sz(v1);
    if(sz(v1) > 0){
        sort(v1.begin(), v1.end());
    }

	multiset <int> st1, st2;
	vector <int> p(sz(v1),0), f(sz(v1),0);
	int sm1 = 0, sm2 = 0;
	for(int j = 0; j < sz(v1); j++){
		pair <int,pair<int,int>> i = v1[j];
		st1.insert(i.ss.ff);
		st1.insert(i.ss.ss);
		sm1 += (i.ss.ff);
		sm1 += (i.ss.ss);
		st2.insert(*st1.rbegin());
		sm2 += *st1.rbegin();
		sm1 -= *(--st1.end());
		st1.erase(--st1.end());
		sm2 += *st1.rbegin();
		st2.insert(*st1.rbegin());
		sm1 -= *(--st1.end());
		st1.erase(--st1.end());
		sm1 += *st2.begin();
		st1.insert(*st2.begin());
		sm2 -= *(st2.begin());
		st2.erase(st2.begin());
		int med = (*st1.rbegin());
		p[j] = (sz(st1)*med - sm1);
		p[j] += (sm2 - med*sz(st2));
	}
    st1.clear();
    st2.clear();
    sm1 = sm2 = 0;
	for(int j = sz(v1)-1; j >= 0; j--){
		pair <int,pair<int,int>> i = v1[j];
		st1.insert(i.ss.ff);
		st1.insert(i.ss.ss);
		sm1 += (i.ss.ff);
		sm1 += (i.ss.ss);
		st2.insert(*st1.rbegin());
		sm2 += *st1.rbegin();
		sm1 -= *(--st1.end());
		st1.erase(--st1.end());
		sm2 += *st1.rbegin();
		st2.insert(*st1.rbegin());
		sm1 -= *(--st1.end());
		st1.erase(--st1.end());
		sm1 += *st2.begin();
		st1.insert(*st2.begin());
		sm2 -= *(st2.begin());
		st2.erase(st2.begin());
		int med = (*st1.rbegin());
		f[j] = (sz(st1)*med - sm1);
		f[j] += (sm2 - med*sz(st2));
	}

	int mn = 0;
    if(sz(v1) > 0) mn = min(p[sz(v1)-1],f[0]);
	for(int i = 0; i < sz(v1)-1; i++){
		mn = min(mn,p[i]+f[i+1]);
	}
    cout << ans + mn << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 2588 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 16 ms 6252 KB Output is correct
13 Correct 33 ms 7816 KB Output is correct
14 Correct 22 ms 6612 KB Output is correct
15 Correct 18 ms 5592 KB Output is correct
16 Correct 19 ms 7192 KB Output is correct
17 Correct 25 ms 7892 KB Output is correct
18 Correct 27 ms 6284 KB Output is correct
19 Correct 30 ms 6604 KB Output is correct
20 Correct 20 ms 6212 KB Output is correct
21 Correct 25 ms 6432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2528 KB Output is correct
16 Correct 1 ms 2512 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2520 KB Output is correct
19 Correct 2 ms 4700 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2524 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 4580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2516 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4448 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2476 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 2 ms 760 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 2 ms 4448 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4708 KB Output is correct
20 Correct 3 ms 4604 KB Output is correct
21 Correct 1 ms 4708 KB Output is correct
22 Correct 2 ms 4600 KB Output is correct
23 Correct 2 ms 4708 KB Output is correct
24 Correct 2 ms 4736 KB Output is correct
25 Correct 173 ms 19176 KB Output is correct
26 Correct 161 ms 19140 KB Output is correct
27 Correct 221 ms 19560 KB Output is correct
28 Correct 235 ms 20292 KB Output is correct
29 Correct 234 ms 19020 KB Output is correct
30 Correct 101 ms 12684 KB Output is correct
31 Correct 184 ms 18212 KB Output is correct
32 Correct 170 ms 18800 KB Output is correct
33 Correct 165 ms 19896 KB Output is correct
34 Correct 161 ms 18876 KB Output is correct
35 Correct 178 ms 18332 KB Output is correct
36 Correct 166 ms 18624 KB Output is correct
37 Correct 146 ms 17344 KB Output is correct