답안 #745391

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
745391 2023-05-20T00:46:31 Z CSQ31 메기 농장 (IOI22_fish) C++17
100 / 100
284 ms 81228 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const ll INF = 1e18;
const int MAXN = 2e5+5;
vector<pii>fish[MAXN];
vector<ll>dp[2][MAXN],crd[MAXN],pref[MAXN];
void chmax(ll &x,ll y){
	x = max(x,y);
}
long long max_weights(int n, int m, vector<int> X, vector<int> Y,vector<int> W) {
	ll ans = 0;
	for(int i=0;i<m;i++)fish[X[i]].pb({Y[i],W[i]});
	for(int i=0;i<n;i++)sort(all(fish[i]));
	for(int i=0;i<n;i++){
		for(pii z:fish[i])crd[i].pb(z.fi);
		if(i)for(pii z:fish[i-1])crd[i].pb(z.fi);
		if(i+1<n)for(pii z:fish[i+1])crd[i].pb(z.fi);
		
		crd[i].pb(-1);
		sort(all(crd[i]));
		crd[i].resize(unique(all(crd[i])) - crd[i].begin());
		int s = sz(crd[i]);
		
		dp[0][i].assign(s,-INF);
		dp[1][i].assign(s,-INF);
		
		pref[i].assign(s,0);
		for(pii z:fish[i]){
			int p = lb(all(crd[i]),z.fi) - crd[i].begin();
			pref[i][p] = z.se;
		}
		for(int j=1;j<s;j++)pref[i][j]+=pref[i][j-1];
	}
	
	//initialize column one 
	for(ll &x:dp[0][0])x = 0;
	dp[1][0][sz(crd[0])-1] = 0;
	for(int i=1;i<n;i++){
		//last pier is lower
		ll mx = -INF;
		int ptr = -1;
		for(int j=0;j<sz(crd[i]);j++){
			while(ptr + 1 < sz(crd[i-1]) && crd[i-1][ptr+1] <= crd[i][j]){
				ptr++;
				chmax(mx,dp[0][i-1][ptr] - pref[i-1][ptr]);
			}
			chmax(dp[0][i][j],mx + pref[i-1][ptr]);
		}
		//;ast pier is higher
		mx = -INF;
		ptr = sz(crd[i-1]);
		ll sum = pref[i].back();
		vector<pii>f = fish[i];
		for(int j=sz(crd[i])-1;j>=0;j--){
			while(ptr - 1 >= 0 && crd[i-1][ptr-1] >= crd[i][j]){
				ptr--;
				while(!f.empty() && f.back().fi > crd[i-1][ptr]){
					sum-=f.back().se;
					f.pop_back();
				}
				chmax(mx,dp[1][i-1][ptr] + sum);
				chmax(mx,dp[0][i-1][ptr] + sum);
			}
			chmax(dp[1][i][j],mx - pref[i][j]);
		}
		if(i>=1){
			//i-1 column is empty :
			//i-2 pier is shorter
			sum = 0;
			mx = -INF;
			int ptr0 = -1; //column i-2
			int ptr1 = -1; //column i-1
			for(int j=0;j<sz(crd[i]);j++){
				while(ptr0 + 1 < sz(crd[i-2]) && crd[i-2][ptr0+1] <= crd[i][j]){
					ptr0++;
					mx = max(mx,dp[0][i-2][ptr0]);
					mx = max(mx,dp[1][i-2][ptr0]);
				}
				while(ptr1 + 1 < sz(fish[i-1]) && fish[i-1][ptr1+1].fi <= crd[i][j]){
					ptr1++;
					sum+=fish[i-1][ptr1].se;
				}
				chmax(dp[0][i][j],mx + sum);
			}
			//i-2 pier is higher
			f = fish[i-1];
			sum = pref[i-1].back();
			mx = -INF;
			ptr0 = sz(crd[i-2]);
			ptr1 = sz(f) - 1;
			for(int j=sz(crd[i])-1;j>=0;j--){
				while(ptr0 - 1 >= 0 && crd[i-2][ptr0-1] >= crd[i][j]){
					ptr0--;
					while(ptr1 >= 0 && f[ptr1].fi > crd[i-2][ptr0]){
						sum-=f[ptr1].se;
						ptr1--;
					}
					chmax(mx,dp[0][i-2][ptr0] + sum);
					chmax(mx,dp[1][i-2][ptr0] + sum);
				}

				chmax(dp[0][i][j],mx);
			}
		}
		/*
		cout<<"at "<<i<<'\n';
		for(int x:crd[i])cout<<x<<" ";cout<<'\n';
		for(ll x:dp[0][i])cout<<x<<" ";cout<<'\n';
		for(ll x:dp[1][i])cout<<x<<" ";cout<<'\n';
		* */
		
		
	}
	for(ll x:dp[0][n-1])ans = max(ans,x);
	for(ll x:dp[1][n-1])ans = max(ans,x);
	if(n>=2){
		int ptr = -1;
		ll sum = 0;
		for(int i=0;i<sz(crd[n-2]);i++){
			while(ptr + 1 < sz(fish[n-1]) && fish[n-1][ptr+1].fi <= crd[n-2][i]){
				ptr++;
				sum+=fish[n-1][ptr].se;
			}
			chmax(ans,dp[0][n-2][i]+sum);
			chmax(ans,dp[1][n-2][i]+sum);
		}
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 43172 KB Output is correct
2 Correct 92 ms 46452 KB Output is correct
3 Correct 33 ms 36288 KB Output is correct
4 Correct 33 ms 36216 KB Output is correct
5 Correct 247 ms 74676 KB Output is correct
6 Correct 284 ms 74036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 140 ms 50668 KB Output is correct
3 Correct 165 ms 54448 KB Output is correct
4 Correct 82 ms 43220 KB Output is correct
5 Correct 94 ms 46516 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 34 ms 36272 KB Output is correct
11 Correct 34 ms 36248 KB Output is correct
12 Correct 90 ms 45848 KB Output is correct
13 Correct 101 ms 49600 KB Output is correct
14 Correct 84 ms 44500 KB Output is correct
15 Correct 96 ms 44552 KB Output is correct
16 Correct 87 ms 44520 KB Output is correct
17 Correct 95 ms 46844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 36180 KB Output is correct
2 Correct 33 ms 36256 KB Output is correct
3 Correct 68 ms 38808 KB Output is correct
4 Correct 54 ms 38772 KB Output is correct
5 Correct 91 ms 43268 KB Output is correct
6 Correct 91 ms 43384 KB Output is correct
7 Correct 91 ms 43244 KB Output is correct
8 Correct 90 ms 43272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 11 ms 23700 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23792 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 13 ms 23996 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 13 ms 23896 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 11 ms 23700 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23792 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 13 ms 23996 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 13 ms 23896 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 12 ms 23868 KB Output is correct
16 Correct 13 ms 24004 KB Output is correct
17 Correct 33 ms 29308 KB Output is correct
18 Correct 34 ms 29180 KB Output is correct
19 Correct 31 ms 28920 KB Output is correct
20 Correct 32 ms 28788 KB Output is correct
21 Correct 30 ms 28752 KB Output is correct
22 Correct 48 ms 33772 KB Output is correct
23 Correct 17 ms 25044 KB Output is correct
24 Correct 27 ms 28108 KB Output is correct
25 Correct 13 ms 23892 KB Output is correct
26 Correct 17 ms 24952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 11 ms 23700 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23792 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 13 ms 23996 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 13 ms 23896 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 12 ms 23868 KB Output is correct
16 Correct 13 ms 24004 KB Output is correct
17 Correct 33 ms 29308 KB Output is correct
18 Correct 34 ms 29180 KB Output is correct
19 Correct 31 ms 28920 KB Output is correct
20 Correct 32 ms 28788 KB Output is correct
21 Correct 30 ms 28752 KB Output is correct
22 Correct 48 ms 33772 KB Output is correct
23 Correct 17 ms 25044 KB Output is correct
24 Correct 27 ms 28108 KB Output is correct
25 Correct 13 ms 23892 KB Output is correct
26 Correct 17 ms 24952 KB Output is correct
27 Correct 14 ms 24444 KB Output is correct
28 Correct 113 ms 48700 KB Output is correct
29 Correct 172 ms 60436 KB Output is correct
30 Correct 194 ms 72176 KB Output is correct
31 Correct 194 ms 71700 KB Output is correct
32 Correct 137 ms 58512 KB Output is correct
33 Correct 199 ms 72484 KB Output is correct
34 Correct 212 ms 72372 KB Output is correct
35 Correct 78 ms 40116 KB Output is correct
36 Correct 185 ms 67064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 36180 KB Output is correct
2 Correct 33 ms 36256 KB Output is correct
3 Correct 68 ms 38808 KB Output is correct
4 Correct 54 ms 38772 KB Output is correct
5 Correct 91 ms 43268 KB Output is correct
6 Correct 91 ms 43384 KB Output is correct
7 Correct 91 ms 43244 KB Output is correct
8 Correct 90 ms 43272 KB Output is correct
9 Correct 96 ms 48048 KB Output is correct
10 Correct 69 ms 36212 KB Output is correct
11 Correct 132 ms 52216 KB Output is correct
12 Correct 11 ms 23764 KB Output is correct
13 Correct 13 ms 23844 KB Output is correct
14 Correct 12 ms 23788 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 13 ms 23724 KB Output is correct
18 Correct 34 ms 36320 KB Output is correct
19 Correct 33 ms 36296 KB Output is correct
20 Correct 33 ms 36216 KB Output is correct
21 Correct 33 ms 36236 KB Output is correct
22 Correct 118 ms 48884 KB Output is correct
23 Correct 156 ms 57732 KB Output is correct
24 Correct 163 ms 58660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 43172 KB Output is correct
2 Correct 92 ms 46452 KB Output is correct
3 Correct 33 ms 36288 KB Output is correct
4 Correct 33 ms 36216 KB Output is correct
5 Correct 247 ms 74676 KB Output is correct
6 Correct 284 ms 74036 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 140 ms 50668 KB Output is correct
9 Correct 165 ms 54448 KB Output is correct
10 Correct 82 ms 43220 KB Output is correct
11 Correct 94 ms 46516 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 12 ms 23764 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 34 ms 36272 KB Output is correct
17 Correct 34 ms 36248 KB Output is correct
18 Correct 90 ms 45848 KB Output is correct
19 Correct 101 ms 49600 KB Output is correct
20 Correct 84 ms 44500 KB Output is correct
21 Correct 96 ms 44552 KB Output is correct
22 Correct 87 ms 44520 KB Output is correct
23 Correct 95 ms 46844 KB Output is correct
24 Correct 37 ms 36180 KB Output is correct
25 Correct 33 ms 36256 KB Output is correct
26 Correct 68 ms 38808 KB Output is correct
27 Correct 54 ms 38772 KB Output is correct
28 Correct 91 ms 43268 KB Output is correct
29 Correct 91 ms 43384 KB Output is correct
30 Correct 91 ms 43244 KB Output is correct
31 Correct 90 ms 43272 KB Output is correct
32 Correct 13 ms 23892 KB Output is correct
33 Correct 11 ms 23764 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 11 ms 23764 KB Output is correct
36 Correct 11 ms 23700 KB Output is correct
37 Correct 12 ms 23764 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 12 ms 23792 KB Output is correct
40 Correct 12 ms 23732 KB Output is correct
41 Correct 13 ms 23996 KB Output is correct
42 Correct 13 ms 23892 KB Output is correct
43 Correct 13 ms 23896 KB Output is correct
44 Correct 12 ms 23764 KB Output is correct
45 Correct 13 ms 23892 KB Output is correct
46 Correct 12 ms 23868 KB Output is correct
47 Correct 13 ms 24004 KB Output is correct
48 Correct 33 ms 29308 KB Output is correct
49 Correct 34 ms 29180 KB Output is correct
50 Correct 31 ms 28920 KB Output is correct
51 Correct 32 ms 28788 KB Output is correct
52 Correct 30 ms 28752 KB Output is correct
53 Correct 48 ms 33772 KB Output is correct
54 Correct 17 ms 25044 KB Output is correct
55 Correct 27 ms 28108 KB Output is correct
56 Correct 13 ms 23892 KB Output is correct
57 Correct 17 ms 24952 KB Output is correct
58 Correct 14 ms 24444 KB Output is correct
59 Correct 113 ms 48700 KB Output is correct
60 Correct 172 ms 60436 KB Output is correct
61 Correct 194 ms 72176 KB Output is correct
62 Correct 194 ms 71700 KB Output is correct
63 Correct 137 ms 58512 KB Output is correct
64 Correct 199 ms 72484 KB Output is correct
65 Correct 212 ms 72372 KB Output is correct
66 Correct 78 ms 40116 KB Output is correct
67 Correct 185 ms 67064 KB Output is correct
68 Correct 96 ms 48048 KB Output is correct
69 Correct 69 ms 36212 KB Output is correct
70 Correct 132 ms 52216 KB Output is correct
71 Correct 11 ms 23764 KB Output is correct
72 Correct 13 ms 23844 KB Output is correct
73 Correct 12 ms 23788 KB Output is correct
74 Correct 11 ms 23764 KB Output is correct
75 Correct 11 ms 23764 KB Output is correct
76 Correct 13 ms 23724 KB Output is correct
77 Correct 34 ms 36320 KB Output is correct
78 Correct 33 ms 36296 KB Output is correct
79 Correct 33 ms 36216 KB Output is correct
80 Correct 33 ms 36236 KB Output is correct
81 Correct 118 ms 48884 KB Output is correct
82 Correct 156 ms 57732 KB Output is correct
83 Correct 163 ms 58660 KB Output is correct
84 Correct 238 ms 76632 KB Output is correct
85 Correct 242 ms 77044 KB Output is correct
86 Correct 270 ms 79840 KB Output is correct
87 Correct 281 ms 81228 KB Output is correct
88 Correct 12 ms 23888 KB Output is correct
89 Correct 278 ms 81168 KB Output is correct
90 Correct 239 ms 76216 KB Output is correct
91 Correct 199 ms 70304 KB Output is correct