답안 #625923

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
625923 2022-08-11T02:39:25 Z oolimry 메기 농장 (IOI22_fish) C++17
100 / 100
396 ms 58488 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
typedef long long lint;
typedef pair<lint,lint> ii;

struct state{
	lint h, P, D, I;
	bool operator < (state &b){
		return this->h < b.h;
	}
	bool operator == (state &b){
		return this->h == b.h;
	}
};

vector<state> dp[200005];
vector<ii> sum[200005];

///get the sum of values in column x, up to height H
inline lint getsum(int x, lint H){
	if(x == -1) return 0;
	auto it = upper_bound(all(sum[x]), ii(H, 1023456678901234LL));
	if(it == sum[x].begin()) return 0;
	
	it--;
	return it->second;
}


long long max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> C){
	if(n == 1) return 0;
	
	///setting up sum counts
	for(int i = 0;i < m;i++){
		sum[X[i]].push_back(ii(Y[i], C[i]));
	}
	
	for(int i = 0;i < n;i++){
		sort(all(sum[i]));
		for(int j = 1;j < sz(sum[i]);j++){
			sum[i][j].second += sum[i][j-1].second;
		}
	}
	
	
	
	///set up the important values in the dp table
	for(int i = 0;i < m;i++){
		if(X[i] != 0) dp[X[i]-1].push_back({Y[i], 0, 0, 0});
		if(X[i] != n-1) dp[X[i]+1].push_back({Y[i], 0, 0, 0});
	}
	
	for(int i = 0;i < n;i++){
		dp[i].push_back({-1,0,0,0}); ///represent not taking anything
		sort(all(dp[i]));
		dp[i].erase(unique(all(dp[i])), dp[i].end());		
	}
	
	///now do dp
	for(auto &s : dp[0]){
		s.P = getsum(1, s.h);
		s.I = -getsum(0, s.h);
		s.D = getsum(1, s.h) - getsum(0, s.h);
	}
	
	for(int i = 1;i < n;i++){
		
		///I to I and I to P
		lint bestI = 0;
		auto it = dp[i-1].begin();
		for(auto &s : dp[i]){
			while(it != dp[i-1].end()){
				if(it->h <= s.h){
					bestI = max(bestI, it->I);
					it++;
				}
				else break;
			}
			
			///I to I
			s.I = max(s.I, bestI + getsum(i-1, s.h) - getsum(i, s.h));
			
			///I to P
			s.P = max(s.P, bestI + getsum(i-1, s.h) + getsum(i+1, s.h));
		}
		
		///D to D and P to D
		reverse(all(dp[i]));
		reverse(all(dp[i-1]));
		
		lint bestPD = 0;
		it = dp[i-1].begin();
		for(auto &s : dp[i]){
			while(it != dp[i-1].end()){
				//if(i == 1) show2(it->h, it->P);

				if(it->h >= s.h){
					bestPD = max(bestPD, it->P);
					bestPD = max(bestPD, it->D);
					it++;
				}
				else break;
			}
			
			///D to D and P to D
			s.D = max(s.D, bestPD + getsum(i+1, s.h) - getsum(i, s.h));
			//if(i == 1) show2(s.h, bestPD);
		}
		
		reverse(all(dp[i]));
		reverse(all(dp[i-1]));
		
		///D -1 to I -1
		dp[i][0].I = max(dp[i][0].I, dp[i-1][0].D);
		
		///P to P 2 spaces away
		if(i >= 2){
			lint bestP = 0;
			for(auto &s : dp[i-2]) bestP = max(bestP, s.P);
			
			for(auto &s : dp[i]){
				s.P = max(s.P, bestP + getsum(i+1, s.h));
			}
		}
	}
	
	lint ans = 0;
	
	for(int i = 0;i < n;i++){

		for(auto &s : dp[i]){
			ans = max(ans, s.P);
			ans = max(ans, s.I);
			ans = max(ans, s.D);
			
			//show3(i, s.h, s.P);
			//show3(i, s.h, s.I);
			//show3(i, s.h, s.D);
		}
	}

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 19588 KB Output is correct
2 Correct 83 ms 21380 KB Output is correct
3 Correct 14 ms 14380 KB Output is correct
4 Correct 14 ms 14292 KB Output is correct
5 Correct 255 ms 47160 KB Output is correct
6 Correct 271 ms 55884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 158 ms 27852 KB Output is correct
3 Correct 168 ms 31708 KB Output is correct
4 Correct 62 ms 19640 KB Output is correct
5 Correct 74 ms 21416 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 13 ms 14292 KB Output is correct
11 Correct 14 ms 14292 KB Output is correct
12 Correct 76 ms 22092 KB Output is correct
13 Correct 93 ms 24552 KB Output is correct
14 Correct 81 ms 20912 KB Output is correct
15 Correct 86 ms 22104 KB Output is correct
16 Correct 72 ms 20944 KB Output is correct
17 Correct 75 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14388 KB Output is correct
2 Correct 14 ms 14292 KB Output is correct
3 Correct 66 ms 22304 KB Output is correct
4 Correct 48 ms 19796 KB Output is correct
5 Correct 96 ms 30796 KB Output is correct
6 Correct 105 ms 30848 KB Output is correct
7 Correct 102 ms 30844 KB Output is correct
8 Correct 110 ms 30896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9608 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9840 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9608 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9840 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9896 KB Output is correct
17 Correct 42 ms 14984 KB Output is correct
18 Correct 42 ms 16928 KB Output is correct
19 Correct 42 ms 16944 KB Output is correct
20 Correct 32 ms 16980 KB Output is correct
21 Correct 32 ms 17076 KB Output is correct
22 Correct 67 ms 23416 KB Output is correct
23 Correct 14 ms 11056 KB Output is correct
24 Correct 34 ms 13700 KB Output is correct
25 Correct 7 ms 9812 KB Output is correct
26 Correct 12 ms 10836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9608 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9840 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9896 KB Output is correct
17 Correct 42 ms 14984 KB Output is correct
18 Correct 42 ms 16928 KB Output is correct
19 Correct 42 ms 16944 KB Output is correct
20 Correct 32 ms 16980 KB Output is correct
21 Correct 32 ms 17076 KB Output is correct
22 Correct 67 ms 23416 KB Output is correct
23 Correct 14 ms 11056 KB Output is correct
24 Correct 34 ms 13700 KB Output is correct
25 Correct 7 ms 9812 KB Output is correct
26 Correct 12 ms 10836 KB Output is correct
27 Correct 7 ms 10324 KB Output is correct
28 Correct 189 ms 41164 KB Output is correct
29 Correct 275 ms 47388 KB Output is correct
30 Correct 305 ms 49688 KB Output is correct
31 Correct 314 ms 49556 KB Output is correct
32 Correct 230 ms 55460 KB Output is correct
33 Correct 316 ms 49700 KB Output is correct
34 Correct 308 ms 49316 KB Output is correct
35 Correct 122 ms 27820 KB Output is correct
36 Correct 320 ms 54808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14388 KB Output is correct
2 Correct 14 ms 14292 KB Output is correct
3 Correct 66 ms 22304 KB Output is correct
4 Correct 48 ms 19796 KB Output is correct
5 Correct 96 ms 30796 KB Output is correct
6 Correct 105 ms 30848 KB Output is correct
7 Correct 102 ms 30844 KB Output is correct
8 Correct 110 ms 30896 KB Output is correct
9 Correct 98 ms 30792 KB Output is correct
10 Correct 90 ms 29328 KB Output is correct
11 Correct 226 ms 48788 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9616 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 5 ms 9660 KB Output is correct
18 Correct 17 ms 14392 KB Output is correct
19 Correct 14 ms 14388 KB Output is correct
20 Correct 14 ms 14336 KB Output is correct
21 Correct 14 ms 14396 KB Output is correct
22 Correct 118 ms 28720 KB Output is correct
23 Correct 202 ms 48920 KB Output is correct
24 Correct 186 ms 48788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 19588 KB Output is correct
2 Correct 83 ms 21380 KB Output is correct
3 Correct 14 ms 14380 KB Output is correct
4 Correct 14 ms 14292 KB Output is correct
5 Correct 255 ms 47160 KB Output is correct
6 Correct 271 ms 55884 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 158 ms 27852 KB Output is correct
9 Correct 168 ms 31708 KB Output is correct
10 Correct 62 ms 19640 KB Output is correct
11 Correct 74 ms 21416 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 13 ms 14292 KB Output is correct
17 Correct 14 ms 14292 KB Output is correct
18 Correct 76 ms 22092 KB Output is correct
19 Correct 93 ms 24552 KB Output is correct
20 Correct 81 ms 20912 KB Output is correct
21 Correct 86 ms 22104 KB Output is correct
22 Correct 72 ms 20944 KB Output is correct
23 Correct 75 ms 22124 KB Output is correct
24 Correct 15 ms 14388 KB Output is correct
25 Correct 14 ms 14292 KB Output is correct
26 Correct 66 ms 22304 KB Output is correct
27 Correct 48 ms 19796 KB Output is correct
28 Correct 96 ms 30796 KB Output is correct
29 Correct 105 ms 30848 KB Output is correct
30 Correct 102 ms 30844 KB Output is correct
31 Correct 110 ms 30896 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 5 ms 9684 KB Output is correct
35 Correct 6 ms 9684 KB Output is correct
36 Correct 6 ms 9684 KB Output is correct
37 Correct 6 ms 9608 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 5 ms 9684 KB Output is correct
40 Correct 5 ms 9812 KB Output is correct
41 Correct 7 ms 10068 KB Output is correct
42 Correct 6 ms 9812 KB Output is correct
43 Correct 6 ms 9840 KB Output is correct
44 Correct 5 ms 9684 KB Output is correct
45 Correct 6 ms 9812 KB Output is correct
46 Correct 5 ms 9684 KB Output is correct
47 Correct 7 ms 9896 KB Output is correct
48 Correct 42 ms 14984 KB Output is correct
49 Correct 42 ms 16928 KB Output is correct
50 Correct 42 ms 16944 KB Output is correct
51 Correct 32 ms 16980 KB Output is correct
52 Correct 32 ms 17076 KB Output is correct
53 Correct 67 ms 23416 KB Output is correct
54 Correct 14 ms 11056 KB Output is correct
55 Correct 34 ms 13700 KB Output is correct
56 Correct 7 ms 9812 KB Output is correct
57 Correct 12 ms 10836 KB Output is correct
58 Correct 7 ms 10324 KB Output is correct
59 Correct 189 ms 41164 KB Output is correct
60 Correct 275 ms 47388 KB Output is correct
61 Correct 305 ms 49688 KB Output is correct
62 Correct 314 ms 49556 KB Output is correct
63 Correct 230 ms 55460 KB Output is correct
64 Correct 316 ms 49700 KB Output is correct
65 Correct 308 ms 49316 KB Output is correct
66 Correct 122 ms 27820 KB Output is correct
67 Correct 320 ms 54808 KB Output is correct
68 Correct 98 ms 30792 KB Output is correct
69 Correct 90 ms 29328 KB Output is correct
70 Correct 226 ms 48788 KB Output is correct
71 Correct 5 ms 9684 KB Output is correct
72 Correct 4 ms 9684 KB Output is correct
73 Correct 5 ms 9616 KB Output is correct
74 Correct 5 ms 9684 KB Output is correct
75 Correct 4 ms 9684 KB Output is correct
76 Correct 5 ms 9660 KB Output is correct
77 Correct 17 ms 14392 KB Output is correct
78 Correct 14 ms 14388 KB Output is correct
79 Correct 14 ms 14336 KB Output is correct
80 Correct 14 ms 14396 KB Output is correct
81 Correct 118 ms 28720 KB Output is correct
82 Correct 202 ms 48920 KB Output is correct
83 Correct 186 ms 48788 KB Output is correct
84 Correct 371 ms 48112 KB Output is correct
85 Correct 396 ms 47228 KB Output is correct
86 Correct 385 ms 58488 KB Output is correct
87 Correct 345 ms 58280 KB Output is correct
88 Correct 5 ms 9684 KB Output is correct
89 Correct 352 ms 58396 KB Output is correct
90 Correct 303 ms 56972 KB Output is correct
91 Correct 267 ms 56100 KB Output is correct