Submission #64278

#TimeUsernameProblemLanguageResultExecution timeMemory
64278kingpig9Palembang Bridges (APIO15_bridge)C++11
100 / 100
602 ms57496 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

bool cmp (pii p1, pii p2) {
	return p1.fi + p1.se < p2.fi + p2.se;
}

int K, N;
vector<pll> A;

void go (vector<ll> &vres) {
	set<pll> st = {pll(A[0].fi, 0), pll(A[0].se, 1)};
	auto it = st.begin();

	ll ans = A[0].se - A[0].fi;
	vres.push_back(ans);

	for (int i = 1; i < A.size(); i++) {
		ll x = A[i].fi, y = A[i].se;
		pll px(x, 2 * i), py(y, 2 * i + 1);
		st.insert(px);
		st.insert(py);
		if (py < *it) {
			ans += 2 * it->fi - x - y;
			it--;
		} else if (px > *it) {
			it++;
			ans += x + y - 2 * it->fi;
		} else {
			ans += y - x;
		}
		vres.push_back(ans);
	}
}

vector<ll> res[2];

int main() {
	scanf("%d %d", &K, &N);

	ll ans = 0;
	for (int i = 0; i < N; i++) {
		int x, y;
		char c, d;
		scanf(" %c %d %c %d", &c, &x, &d, &y);
		if (x > y) {
			swap(x, y);
		}

		if (c == d) {
			ans += y - x;
		} else {
			ans++;
			A.push_back({x, y});
		}
	}

	if (!A.empty()) {
		sort(all(A), cmp);
		go(res[0]);
		if (K == 1) {
			printf("%lld\n", ans + res[0].back());
			return 0;
		}

		reverse(all(A));
		go(res[1]);
		ll mnadd = min(res[0].back(), res[1].back());
		for (int i = 0; i + 1 < A.size(); i++) {
			mnadd = min(mnadd, res[0][i] + res[1][A.size() - 2 - i]);
		}
		ans += mnadd;
	}
	printf("%lld\n", ans);
}

Compilation message (stderr)

bridge.cpp: In function 'void go(std::vector<long long int>&)':
bridge.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < A.size(); i++) {
                  ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:80:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i + 1 < A.size(); i++) {
                   ~~~~~~^~~~~~~~~~
bridge.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &K, &N);
  ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c %d %c %d", &c, &x, &d, &y);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...