답안 #1057190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057190 2024-08-13T14:55:24 Z dozer 메기 농장 (IOI22_fish) C++17
100 / 100
257 ms 81108 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define ll long long
#define LL node * 2
#define RR node * 2 + 1
#define MAXN 600005

const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;


ll start[MAXN], dp[2][MAXN];
ll pref[MAXN], suff[MAXN];

vector<pii> fish[MAXN];


long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
	int n = N, m = M;
	vector<array<int, 3>> v;
	map<pii, int> done;
	for (int i = 0; i < m; i++) done[{X[i], Y[i]}] = 1;
	for (int i = 0; i < n; i++){
		if (done.count({i, 0}) == 0){
			X.pb(i), Y.pb(0), W.pb(0);
		}
	}

	m = X.size();
	for (int i = 0; i < m; i++) v.pb({X[i] + 1, Y[i], W[i]});
	sort(v.begin(), v.end());
    vector<int> x(m + 5), y(m + 5), w(m + 5);
    for (int i = 1; i <= m; i++) x[i] = v[i - 1][0], y[i] = v[i - 1][1], w[i] = v[i - 1][2];
    for (int i = 1; i <= m; i++) fish[x[i]].pb({y[i], i});

    vector<ll> pre(m + 5, 0);
	for (int i = 1; i <= m; i++) pre[i] = pre[i - 1] + w[i];

	for (int i = n; i >= 1; i--){
		start[i] = max(start[i + 1], start[i + 2] + pre[fish[i].back().nd] - pre[fish[i].front().nd - 1]);

		if (i == n) {
			for (auto j : fish[i]){
				int k = j.nd, b = j.st;
				pref[k] = -pre[k - 1];
				int tmp = lower_bound(fish[i - 1].begin(), fish[i - 1].end(), make_pair(b, 0)) - fish[i - 1].begin();
				tmp--;
				if (tmp >= 0){
					int to2 = fish[i - 1][tmp].nd;
					suff[k] = dp[1][k] + pre[to2];
				}

				for (int j = fish[i].front().nd + 1; j <= fish[i].back().nd; j++) pref[j] = max(pref[j - 1], pref[j]);
				for (int j = fish[i].back().nd - 1; j >= fish[i].front().nd; j--) suff[j] = max(suff[j], suff[j + 1]);
			}
			continue;
		}


		for (auto J = 0; J < fish[i].size(); J++){
			pii j = fish[i][J];
			int k = j.nd;
			int a = x[k], b = y[k];

			//todo : continue decreasing

			int to = lower_bound(fish[i + 1].begin(), fish[i + 1].end(), make_pair(b, 0)) - fish[i + 1].begin();
			to--;
			if (to >= 0){
				int to2 = fish[i + 1][to].nd;
				int gh = lower_bound(fish[i + 1].begin(), fish[i + 1].end(), make_pair(b + 1, 0)) - fish[i + 1].begin();
				gh--;
				if (gh >= 0){
					dp[0][k] = max(dp[0][k], pref[fish[i + 1][gh].nd] + pre[to2]);
				}
			}

			//todo : switch to increasing

			if (b == 0){
				dp[0][k] = max(dp[0][k], dp[1][fish[i + 1].front().nd]);
			}

			//////////////////////////////////////////

			//todo : continue increasing
			int gh = lower_bound(fish[i + 1].begin(), fish[i + 1].end(), make_pair(b, 0)) - fish[i + 1].begin();
			if (gh != fish[i + 1].size()){
				dp[1][k] = max(dp[1][k], suff[fish[i + 1][gh].nd] - pre[k - 1]);

			}

			//todo : connect to full column
			dp[1][k] = max(dp[1][k], start[i + 2] + pre[fish[i].back().nd] - pre[k - 1]);


			/////////////////////////////////////////
			pref[k] = dp[0][k] - pre[k - 1];
			int tmp = lower_bound(fish[i - 1].begin(), fish[i - 1].end(), make_pair(b, 0)) - fish[i - 1].begin();
			tmp--;
			if (tmp >= 0){
				int to2 = fish[i - 1][tmp].nd;
				suff[k] = dp[1][k] + pre[to2];
			}
		}
		

		for (int j = 0; j < fish[i].size(); j++){
			int curr = fish[i][j].nd;
			start[i] = max(start[i], dp[0][curr] + pre[fish[i].back().nd] - pre[curr - 1]);
		}

		for (int j = fish[i].front().nd + 1; j <= fish[i].back().nd; j++) pref[j] = max(pref[j - 1], pref[j]);
		for (int j = fish[i].back().nd - 1; j >= fish[i].front().nd; j--) suff[j] = max(suff[j], suff[j + 1]);
	}


	ll ans = start[2];
	for (int i = 1; i <= m; i++) {
		//cout<<i<<sp<<x[i]<<sp<<y[i]<<sp<<w[i]<<" : "<<dp[0][i]<<sp<<dp[1][i]<<endl;
		ans = max(ans, max(dp[0][i], dp[1][i]));
	}
	return ans;
}

/*

int main() {
	fileio();
	int N, M;
	assert(2 == scanf("%d %d", &N, &M));

	std::vector<int> X(M), Y(M), W(M);
	for (int i = 0; i < M; ++i) {
		assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
	}

	long long result = max_weights(N, M, X, Y, W);
	printf("%lld\n", result);
	return 0;
}*/

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:69:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |   for (auto J = 0; J < fish[i].size(); J++){
      |                    ~~^~~~~~~~~~~~~~~~
fish.cpp:97:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |    if (gh != fish[i + 1].size()){
      |        ~~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:72:8: warning: unused variable 'a' [-Wunused-variable]
   72 |    int a = x[k], b = y[k];
      |        ^
fish.cpp:117:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |   for (int j = 0; j < fish[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 39212 KB Output is correct
2 Correct 67 ms 43060 KB Output is correct
3 Correct 13 ms 28744 KB Output is correct
4 Correct 13 ms 28744 KB Output is correct
5 Correct 255 ms 76148 KB Output is correct
6 Correct 228 ms 74376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 119 ms 53464 KB Output is correct
3 Correct 149 ms 61428 KB Output is correct
4 Correct 56 ms 39476 KB Output is correct
5 Correct 66 ms 43060 KB Output is correct
6 Correct 2 ms 16984 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 13 ms 28732 KB Output is correct
11 Correct 13 ms 28772 KB Output is correct
12 Correct 56 ms 40412 KB Output is correct
13 Correct 67 ms 43056 KB Output is correct
14 Correct 61 ms 39444 KB Output is correct
15 Correct 74 ms 41516 KB Output is correct
16 Correct 58 ms 39956 KB Output is correct
17 Correct 67 ms 41704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28740 KB Output is correct
2 Correct 12 ms 28744 KB Output is correct
3 Correct 37 ms 31696 KB Output is correct
4 Correct 32 ms 31428 KB Output is correct
5 Correct 61 ms 35972 KB Output is correct
6 Correct 59 ms 36040 KB Output is correct
7 Correct 61 ms 36008 KB Output is correct
8 Correct 59 ms 36040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 3 ms 17244 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 17244 KB Output is correct
13 Correct 2 ms 16988 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 3 ms 17244 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 17244 KB Output is correct
13 Correct 2 ms 16988 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 3 ms 17244 KB Output is correct
17 Correct 30 ms 23404 KB Output is correct
18 Correct 28 ms 23548 KB Output is correct
19 Correct 26 ms 23552 KB Output is correct
20 Correct 25 ms 23552 KB Output is correct
21 Correct 25 ms 23508 KB Output is correct
22 Correct 54 ms 30356 KB Output is correct
23 Correct 7 ms 18224 KB Output is correct
24 Correct 18 ms 21312 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 6 ms 18228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 3 ms 17244 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 17244 KB Output is correct
13 Correct 2 ms 16988 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 3 ms 17244 KB Output is correct
17 Correct 30 ms 23404 KB Output is correct
18 Correct 28 ms 23548 KB Output is correct
19 Correct 26 ms 23552 KB Output is correct
20 Correct 25 ms 23552 KB Output is correct
21 Correct 25 ms 23508 KB Output is correct
22 Correct 54 ms 30356 KB Output is correct
23 Correct 7 ms 18224 KB Output is correct
24 Correct 18 ms 21312 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 6 ms 18228 KB Output is correct
27 Correct 4 ms 17500 KB Output is correct
28 Correct 158 ms 49912 KB Output is correct
29 Correct 226 ms 63880 KB Output is correct
30 Correct 215 ms 64396 KB Output is correct
31 Correct 221 ms 69668 KB Output is correct
32 Correct 219 ms 71360 KB Output is correct
33 Correct 216 ms 71564 KB Output is correct
34 Correct 218 ms 71312 KB Output is correct
35 Correct 74 ms 36344 KB Output is correct
36 Correct 225 ms 72572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28740 KB Output is correct
2 Correct 12 ms 28744 KB Output is correct
3 Correct 37 ms 31696 KB Output is correct
4 Correct 32 ms 31428 KB Output is correct
5 Correct 61 ms 35972 KB Output is correct
6 Correct 59 ms 36040 KB Output is correct
7 Correct 61 ms 36008 KB Output is correct
8 Correct 59 ms 36040 KB Output is correct
9 Correct 82 ms 43056 KB Output is correct
10 Correct 61 ms 34400 KB Output is correct
11 Correct 132 ms 50880 KB Output is correct
12 Correct 2 ms 16988 KB Output is correct
13 Correct 2 ms 16984 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 2 ms 16988 KB Output is correct
17 Correct 2 ms 16984 KB Output is correct
18 Correct 12 ms 28744 KB Output is correct
19 Correct 13 ms 28744 KB Output is correct
20 Correct 12 ms 28744 KB Output is correct
21 Correct 12 ms 28744 KB Output is correct
22 Correct 79 ms 43540 KB Output is correct
23 Correct 152 ms 61596 KB Output is correct
24 Correct 152 ms 62448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 39212 KB Output is correct
2 Correct 67 ms 43060 KB Output is correct
3 Correct 13 ms 28744 KB Output is correct
4 Correct 13 ms 28744 KB Output is correct
5 Correct 255 ms 76148 KB Output is correct
6 Correct 228 ms 74376 KB Output is correct
7 Correct 2 ms 16984 KB Output is correct
8 Correct 119 ms 53464 KB Output is correct
9 Correct 149 ms 61428 KB Output is correct
10 Correct 56 ms 39476 KB Output is correct
11 Correct 66 ms 43060 KB Output is correct
12 Correct 2 ms 16984 KB Output is correct
13 Correct 2 ms 16988 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 13 ms 28732 KB Output is correct
17 Correct 13 ms 28772 KB Output is correct
18 Correct 56 ms 40412 KB Output is correct
19 Correct 67 ms 43056 KB Output is correct
20 Correct 61 ms 39444 KB Output is correct
21 Correct 74 ms 41516 KB Output is correct
22 Correct 58 ms 39956 KB Output is correct
23 Correct 67 ms 41704 KB Output is correct
24 Correct 13 ms 28740 KB Output is correct
25 Correct 12 ms 28744 KB Output is correct
26 Correct 37 ms 31696 KB Output is correct
27 Correct 32 ms 31428 KB Output is correct
28 Correct 61 ms 35972 KB Output is correct
29 Correct 59 ms 36040 KB Output is correct
30 Correct 61 ms 36008 KB Output is correct
31 Correct 59 ms 36040 KB Output is correct
32 Correct 2 ms 16988 KB Output is correct
33 Correct 2 ms 16988 KB Output is correct
34 Correct 2 ms 16988 KB Output is correct
35 Correct 2 ms 16988 KB Output is correct
36 Correct 2 ms 16988 KB Output is correct
37 Correct 2 ms 16988 KB Output is correct
38 Correct 2 ms 16988 KB Output is correct
39 Correct 2 ms 16988 KB Output is correct
40 Correct 2 ms 16988 KB Output is correct
41 Correct 3 ms 17244 KB Output is correct
42 Correct 2 ms 16988 KB Output is correct
43 Correct 2 ms 17244 KB Output is correct
44 Correct 2 ms 16988 KB Output is correct
45 Correct 2 ms 16988 KB Output is correct
46 Correct 2 ms 16988 KB Output is correct
47 Correct 3 ms 17244 KB Output is correct
48 Correct 30 ms 23404 KB Output is correct
49 Correct 28 ms 23548 KB Output is correct
50 Correct 26 ms 23552 KB Output is correct
51 Correct 25 ms 23552 KB Output is correct
52 Correct 25 ms 23508 KB Output is correct
53 Correct 54 ms 30356 KB Output is correct
54 Correct 7 ms 18224 KB Output is correct
55 Correct 18 ms 21312 KB Output is correct
56 Correct 3 ms 17244 KB Output is correct
57 Correct 6 ms 18228 KB Output is correct
58 Correct 4 ms 17500 KB Output is correct
59 Correct 158 ms 49912 KB Output is correct
60 Correct 226 ms 63880 KB Output is correct
61 Correct 215 ms 64396 KB Output is correct
62 Correct 221 ms 69668 KB Output is correct
63 Correct 219 ms 71360 KB Output is correct
64 Correct 216 ms 71564 KB Output is correct
65 Correct 218 ms 71312 KB Output is correct
66 Correct 74 ms 36344 KB Output is correct
67 Correct 225 ms 72572 KB Output is correct
68 Correct 82 ms 43056 KB Output is correct
69 Correct 61 ms 34400 KB Output is correct
70 Correct 132 ms 50880 KB Output is correct
71 Correct 2 ms 16988 KB Output is correct
72 Correct 2 ms 16984 KB Output is correct
73 Correct 2 ms 16988 KB Output is correct
74 Correct 2 ms 16988 KB Output is correct
75 Correct 2 ms 16988 KB Output is correct
76 Correct 2 ms 16984 KB Output is correct
77 Correct 12 ms 28744 KB Output is correct
78 Correct 13 ms 28744 KB Output is correct
79 Correct 12 ms 28744 KB Output is correct
80 Correct 12 ms 28744 KB Output is correct
81 Correct 79 ms 43540 KB Output is correct
82 Correct 152 ms 61596 KB Output is correct
83 Correct 152 ms 62448 KB Output is correct
84 Correct 257 ms 78032 KB Output is correct
85 Correct 242 ms 79760 KB Output is correct
86 Correct 231 ms 80088 KB Output is correct
87 Correct 248 ms 80988 KB Output is correct
88 Correct 2 ms 16984 KB Output is correct
89 Correct 236 ms 81108 KB Output is correct
90 Correct 227 ms 79484 KB Output is correct
91 Correct 224 ms 78952 KB Output is correct