Submission #456224

# Submission time Handle Problem Language Result Execution time Memory
456224 2021-08-06T09:15:45 Z nonsensenonsense1 Palembang Bridges (APIO15_bridge) C++17
100 / 100
341 ms 14728 KB
#include <cstdio>
#include <set>
#include <algorithm>
#include <vector>

const int N = 100000;
int n, task;
long long pref[N + 1], suf[N + 1];
std::vector<std::pair<int, std::pair<int, int> > > a;

void process(long long *to) 
{
	std::multiset<int> x, y;
	long long sx = 0, sy = 0;
	for (int i = 0; i < (int)a.size(); ++i) {
		x.insert(a[i].second.first);
		x.insert(a[i].second.second);
		sx += a[i].second.first;
		sx += a[i].second.second;
		y.insert(*prev(x.end()));
		sy += *prev(x.end());
		sx -= *prev(x.end());
		x.erase(prev(x.end()));
		while (*prev(x.end()) > *y.begin()) {
			int p = *prev(x.end()), q = *y.begin();
			x.erase(prev(x.end()));
			y.erase(y.begin());
			x.insert(q);
			y.insert(p);
			sx -= p - q;
			sy += p - q;
		}
		to[i + 1] = sy - sx;
	}
}

int main() 
{
	scanf("%d%d", &task, &n);
	long long ans_add = 0;
	for (int i = 0; i < n; ++i) {
		char type1, type2;
		int l, r;
		scanf(" %c%d %c%d", &type1, &l, &type2, &r);
		if (l > r) std::swap(l, r);
		if (type1 == type2) ans_add += r - l;
		else {
			++ans_add;
			a.push_back(std::make_pair(l + r, std::make_pair(l, r)));
		}
	}
	std::sort(a.begin(), a.end());
	process(pref);
	std::reverse(a.begin(), a.end());
	process(suf);
	if (task == 1) printf("%lld\n", ans_add + pref[a.size()]);
	else {
		long long ans = ~((long long)1 << 63);
		for (int i = 0; i <= a.size(); ++i) ans = std::min(ans, pref[i] + suf[(int)a.size() - i]);
		printf("%lld\n", ans_add + ans);
	}
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0; i <= a.size(); ++i) ans = std::min(ans, pref[i] + suf[(int)a.size() - i]);
      |                   ~~^~~~~~~~~~~
bridge.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d%d", &task, &n);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
bridge.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf(" %c%d %c%d", &type1, &l, &type2, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 202 ms 12340 KB Output is correct
13 Correct 291 ms 12348 KB Output is correct
14 Correct 292 ms 11184 KB Output is correct
15 Correct 138 ms 7364 KB Output is correct
16 Correct 175 ms 12300 KB Output is correct
17 Correct 227 ms 12296 KB Output is correct
18 Correct 182 ms 12340 KB Output is correct
19 Correct 215 ms 12348 KB Output is correct
20 Correct 207 ms 12336 KB Output is correct
21 Correct 229 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 272 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 288 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 288 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 3 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 432 KB Output is correct
21 Correct 2 ms 432 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 201 ms 13160 KB Output is correct
26 Correct 214 ms 13348 KB Output is correct
27 Correct 306 ms 14096 KB Output is correct
28 Correct 308 ms 14728 KB Output is correct
29 Correct 341 ms 14728 KB Output is correct
30 Correct 129 ms 7904 KB Output is correct
31 Correct 181 ms 14000 KB Output is correct
32 Correct 222 ms 14632 KB Output is correct
33 Correct 193 ms 14252 KB Output is correct
34 Correct 229 ms 14628 KB Output is correct
35 Correct 225 ms 14220 KB Output is correct
36 Correct 224 ms 14516 KB Output is correct
37 Correct 176 ms 13168 KB Output is correct