답안 #1071406

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1071406 2024-08-23T07:11:11 Z 정민찬(#11138) Cultivation (JOI17_cultivation) C++17
80 / 100
2000 ms 2520 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

const ll inf = 2e18;

ll R, C;
ll N;

ll solve(vector<pll> p, ll H) {
	vector<pll> upd;
	ll j = 0;
	for (ll i=0; i<p.size(); i++) {
		while (j < p.size() && p[j].first + H <= p[i].first) {
			upd.push_back({p[j].first + H, -p[j].second});
			j ++;
		}
		upd.push_back(p[i]);
	}
	while (j < p.size()) {
		upd.push_back({p[j].first + H, -p[j].second});
		j ++;
	}
	vector<array<ll,4>> save;
	multiset<ll> s1, s2;
	for (ll i=0; i<upd.size(); i++) {
		if (upd[i].second > 0) {
			if (!s1.empty()) {
				auto rit = s1.lower_bound(upd[i].second);
				if (rit != s1.begin()) {
					auto lit = prev(rit);
					if (rit != s1.end()) {
						s2.erase(s2.find(*rit - *lit));
					}
					s2.insert(upd[i].second - *lit);
				}
				if (rit != s1.end()) {
					s2.insert(*rit - upd[i].second);
				}
			}
			s1.insert(upd[i].second);
		}
		else {
			s1.erase(s1.find(-upd[i].second));
			if (!s1.empty()) {
				auto rit = s1.lower_bound(-upd[i].second);
				if (rit != s1.begin()) {
					auto lit = prev(rit);
					if (rit != s1.end()) {
						s2.insert(*rit - *lit);
					}
					s2.erase(s2.find(-upd[i].second - *lit));
				}
				if (rit != s1.end()) {
					s2.erase(s2.find(*rit - (-upd[i].second)));
				}
			}
		}
		if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
		if (s1.empty()) {
			save.push_back({upd[i].first, inf, inf, inf});
			continue;
		}
		ll l = *s1.begin() - 1, r = C - *s1.rbegin();
		ll sum = l + r;
		if (!s2.empty()) sum = *s2.rbegin() - 1;
		save.push_back({upd[i].first, sum, l, r});
	}
	ll ans = inf;
	for (ll i=0; i<save.size(); i++) {
		ll l = save[i][2], r = save[i][3];
		ll sum = save[i][1];
		for (ll j=i+1; j<save.size(); j++) {
			if (save[j][0] > save[i][0] + R - 1) break;
			l = max(l, save[j][2]);
			r = max(r, save[j][3]);
			sum = max(sum, save[j][1]);
		}
		ans = min(ans, max(l+r, sum));
	}
	return ans;
}

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    cin >> R >> C;
    cin >> N;
    vector<pll> p(N);
    for (ll i=0; i<N; i++) {
    	cin >> p[i].first >> p[i].second;
    }
    sort(p.begin(), p.end());
    vector<ll> cand, U, D;
    for (ll i=0; i<N; i++) {
    	U.push_back(p[i].first - 1);
    	D.push_back(R - p[i].first);
    	for (ll j=i+1; j<N; j++) {
    		cand.push_back(abs(p[i].first - p[j].first));
    	}
    }
    for (auto &u : U) {
    	for (auto &d : D) {
    		cand.push_back(u + d + 1);
    	}
    }
    sort(cand.begin(), cand.end());
    cand.erase(unique(cand.begin(), cand.end()), cand.end());
    ll ans = inf;
    for (auto &H : cand) {
   		if (H == 0) continue;
    	ans = min(ans, H - 1 + solve(p, H));
    }
    /*for (ll i=1; i<=2*R; i++) {
    	cout << i << ' ' << solve(p, i) << '\n';
    	ans = min(ans, i - 1 + solve(p, i));
    }*/
    cout << ans << '\n';
}

Compilation message

cultivation.cpp: In function 'll solve(std::vector<std::pair<long long int, long long int> >, ll)':
cultivation.cpp:16:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for (ll i=0; i<p.size(); i++) {
      |               ~^~~~~~~~~
cultivation.cpp:17:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |   while (j < p.size() && p[j].first + H <= p[i].first) {
      |          ~~^~~~~~~~~~
cultivation.cpp:23:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  while (j < p.size()) {
      |         ~~^~~~~~~~~~
cultivation.cpp:29:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |  for (ll i=0; i<upd.size(); i++) {
      |               ~^~~~~~~~~~~
cultivation.cpp:62:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
      |       ~~~~~~^~~~~~~~~~~~~
cultivation.cpp:73:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for (ll i=0; i<save.size(); i++) {
      |               ~^~~~~~~~~~~~
cultivation.cpp:76:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for (ll j=i+1; j<save.size(); j++) {
      |                  ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 424 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 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 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 0 ms 348 KB Output is correct
5 Correct 1 ms 424 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 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 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 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 7 ms 736 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 7 ms 992 KB Output is correct
25 Correct 5 ms 1248 KB Output is correct
26 Correct 12 ms 2520 KB Output is correct
27 Correct 12 ms 2520 KB Output is correct
28 Correct 7 ms 992 KB Output is correct
29 Correct 12 ms 2520 KB Output is correct
30 Correct 15 ms 2516 KB Output is correct
31 Correct 12 ms 2520 KB Output is correct
32 Correct 12 ms 2520 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 0 ms 348 KB Output is correct
5 Correct 1 ms 424 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 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 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 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 7 ms 736 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 7 ms 992 KB Output is correct
25 Correct 5 ms 1248 KB Output is correct
26 Correct 12 ms 2520 KB Output is correct
27 Correct 12 ms 2520 KB Output is correct
28 Correct 7 ms 992 KB Output is correct
29 Correct 12 ms 2520 KB Output is correct
30 Correct 15 ms 2516 KB Output is correct
31 Correct 12 ms 2520 KB Output is correct
32 Correct 12 ms 2520 KB Output is correct
33 Correct 4 ms 2516 KB Output is correct
34 Correct 8 ms 2520 KB Output is correct
35 Correct 13 ms 2516 KB Output is correct
36 Correct 14 ms 2520 KB Output is correct
37 Correct 13 ms 2520 KB Output is correct
38 Correct 13 ms 2520 KB Output is correct
39 Correct 13 ms 2520 KB Output is correct
40 Correct 20 ms 2520 KB Output is correct
41 Correct 9 ms 2520 KB Output is correct
42 Correct 12 ms 2520 KB Output is correct
43 Correct 13 ms 2520 KB Output is correct
44 Correct 13 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 5 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 5 ms 348 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 7 ms 348 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 3 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 6 ms 476 KB Output is correct
17 Correct 6 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 5 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 5 ms 348 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 7 ms 348 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 3 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 6 ms 476 KB Output is correct
17 Correct 6 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
21 Correct 372 ms 604 KB Output is correct
22 Correct 581 ms 676 KB Output is correct
23 Correct 629 ms 604 KB Output is correct
24 Correct 560 ms 680 KB Output is correct
25 Correct 570 ms 680 KB Output is correct
26 Correct 190 ms 604 KB Output is correct
27 Correct 482 ms 676 KB Output is correct
28 Correct 578 ms 684 KB Output is correct
29 Correct 607 ms 684 KB Output is correct
30 Correct 609 ms 676 KB Output is correct
31 Correct 570 ms 692 KB Output is correct
32 Correct 529 ms 680 KB Output is correct
33 Correct 450 ms 604 KB Output is correct
34 Correct 577 ms 604 KB Output is correct
35 Correct 519 ms 604 KB Output is correct
36 Correct 635 ms 676 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 0 ms 348 KB Output is correct
5 Correct 1 ms 424 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 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 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 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 7 ms 736 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 7 ms 992 KB Output is correct
25 Correct 5 ms 1248 KB Output is correct
26 Correct 12 ms 2520 KB Output is correct
27 Correct 12 ms 2520 KB Output is correct
28 Correct 7 ms 992 KB Output is correct
29 Correct 12 ms 2520 KB Output is correct
30 Correct 15 ms 2516 KB Output is correct
31 Correct 12 ms 2520 KB Output is correct
32 Correct 12 ms 2520 KB Output is correct
33 Correct 4 ms 2516 KB Output is correct
34 Correct 8 ms 2520 KB Output is correct
35 Correct 13 ms 2516 KB Output is correct
36 Correct 14 ms 2520 KB Output is correct
37 Correct 13 ms 2520 KB Output is correct
38 Correct 13 ms 2520 KB Output is correct
39 Correct 13 ms 2520 KB Output is correct
40 Correct 20 ms 2520 KB Output is correct
41 Correct 9 ms 2520 KB Output is correct
42 Correct 12 ms 2520 KB Output is correct
43 Correct 13 ms 2520 KB Output is correct
44 Correct 13 ms 2516 KB Output is correct
45 Correct 3 ms 348 KB Output is correct
46 Correct 5 ms 348 KB Output is correct
47 Correct 6 ms 348 KB Output is correct
48 Correct 5 ms 348 KB Output is correct
49 Correct 6 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 5 ms 344 KB Output is correct
53 Correct 3 ms 348 KB Output is correct
54 Correct 5 ms 348 KB Output is correct
55 Correct 7 ms 348 KB Output is correct
56 Correct 7 ms 348 KB Output is correct
57 Correct 2 ms 344 KB Output is correct
58 Correct 3 ms 348 KB Output is correct
59 Correct 2 ms 348 KB Output is correct
60 Correct 6 ms 476 KB Output is correct
61 Correct 6 ms 348 KB Output is correct
62 Correct 3 ms 348 KB Output is correct
63 Correct 6 ms 348 KB Output is correct
64 Correct 4 ms 348 KB Output is correct
65 Correct 372 ms 604 KB Output is correct
66 Correct 581 ms 676 KB Output is correct
67 Correct 629 ms 604 KB Output is correct
68 Correct 560 ms 680 KB Output is correct
69 Correct 570 ms 680 KB Output is correct
70 Correct 190 ms 604 KB Output is correct
71 Correct 482 ms 676 KB Output is correct
72 Correct 578 ms 684 KB Output is correct
73 Correct 607 ms 684 KB Output is correct
74 Correct 609 ms 676 KB Output is correct
75 Correct 570 ms 692 KB Output is correct
76 Correct 529 ms 680 KB Output is correct
77 Correct 450 ms 604 KB Output is correct
78 Correct 577 ms 604 KB Output is correct
79 Correct 519 ms 604 KB Output is correct
80 Correct 635 ms 676 KB Output is correct
81 Execution timed out 2054 ms 1500 KB Time limit exceeded
82 Halted 0 ms 0 KB -