Submission #453395

#TimeUsernameProblemLanguageResultExecution timeMemory
453395nonsensenonsense1Palembang Bridges (APIO15_bridge)C++17
22 / 100
56 ms3488 KiB
#include <cstdio>
#include <vector>

const int N = 1000000000;

int k, n;
std::vector<std::pair<int, int> > a;

long long test2(int x) 
{
	long long s = 0;
	for (int i = 0; i < (int)a.size(); ++i) s += std::max(a[i].first - x, 0) + std::max(x - a[i].second, 0);
	return s;
}

long long test1() 
{
	int l = 0, r = N, opt;
	while (l <= r) {
		int m = l + r >> 1;
		if (test2(m) <= test2(m + 1)) {
			opt = m;
			r = m - 1;
		}
		else l = m + 1;
	}
	return test2(opt);
}

int main() 
{
	long long ans = 0;
	scanf("%d%d", &k, &n);
	for (int i = 0; i < n; ++i) {
		char type_from, type_to;
		int from, to;
		scanf(" %c%d %c%d", &type_from, &from, &type_to, &to);
		if (from > to) std::swap(from, to);
		ans += to - from;
		if (type_from != type_to) {
			a.push_back(std::make_pair(from, to));
			++ans;
		}
	}
	printf("%lld\n", ans + test1() * 2);
	return 0;
}

Compilation message (stderr)

bridge.cpp: In function 'long long int test1()':
bridge.cpp:20:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   20 |   int m = l + r >> 1;
      |           ~~^~~
bridge.cpp: In function 'int main()':
bridge.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |  scanf("%d%d", &k, &n);
      |  ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |   scanf(" %c%d %c%d", &type_from, &from, &type_to, &to);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp: In function 'long long int test1()':
bridge.cpp:27:14: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   27 |  return test2(opt);
      |         ~~~~~^~~~~
#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...