답안 #1077285

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077285 2024-08-27T04:37:57 Z Muhammet Palembang Bridges (APIO15_bridge) C++17
63 / 100
2000 ms 12512 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);
	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);
		st2.insert(*st1.rbegin());
		st1.erase(--st1.end());
		st2.insert(*st1.rbegin());
		st1.erase(--st1.end());
		st1.insert(*st2.begin());
		st2.erase(st2.begin());
		int med = (*st1.rbegin());
		for(int j1 = 0; j1 <= j; j1++){
			p[j] += abs(med-v1[j1].ss.ff);
			p[j] += abs(med-v1[j1].ss.ss);
		}
	}
    st1.clear();
    st2.clear();
	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);
		st2.insert(*st1.rbegin());
		st1.erase(--st1.end());
		st2.insert(*st1.rbegin());
		st1.erase(--st1.end());
		st1.insert(*st2.begin());
		st2.erase(st2.begin());
		int med = (*st1.rbegin());
		for(int j1 = sz(v1)-1; j1 >= j; j1--){
			f[j] += abs(med-v1[j1].ss.ff);
			f[j] += abs(med-v1[j1].ss.ss);
		}
	}

	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 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 484 KB Output is correct
6 Correct 1 ms 476 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 456 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 372 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 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 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 17 ms 5100 KB Output is correct
13 Correct 31 ms 6540 KB Output is correct
14 Correct 23 ms 5332 KB Output is correct
15 Correct 20 ms 4060 KB Output is correct
16 Correct 19 ms 5848 KB Output is correct
17 Correct 22 ms 6644 KB Output is correct
18 Correct 26 ms 6104 KB Output is correct
19 Correct 31 ms 6616 KB Output is correct
20 Correct 20 ms 6100 KB Output is correct
21 Correct 27 ms 6352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 512 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 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 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 1 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 2 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 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 344 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 4 ms 480 KB Output is correct
21 Correct 2 ms 600 KB Output is correct
22 Correct 4 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 360 KB Output is correct
2 Correct 0 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 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 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 1 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 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 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 636 KB Output is correct
22 Correct 3 ms 600 KB Output is correct
23 Correct 2 ms 636 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Execution timed out 2098 ms 12512 KB Time limit exceeded
26 Halted 0 ms 0 KB -