제출 #396280

#제출 시각아이디문제언어결과실행 시간메모리
396280SavicSPalembang Bridges (APIO15_bridge)C++14
0 / 100
158 ms336 KiB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, K;
array<int,4> niz[maxn];

int main() 
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> K >> n;

    vector<int> vec;
    vector<pii> A;

    int rez = 0;
    ff(i,1,n) {
        int S, T;
        char p, q;
        cin >> p >> S >> q >> T;
        int P = p - 'A', Q = q - 'A';

        if(P == Q)rez += abs(T - S);
        else {
            vec.pb(S), vec.pb(T);
            A.pb({S, T});
        }
    }
 
    if(!sz(vec))return cout << rez, 0;
    sort(all(vec));

	int ans = inf;
	ff(c,0,100000){
		int kol = 0;
		for(auto f : A){
			kol += abs(c - f.fi) + abs(c - f.se) + 1;
		}
		ans = min(ans, kol);
	}

	cout << rez + ans << endl;

    return 0;
}
/**

1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

// probati bojenje sahovski ili slicno

**/


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...