#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++;
mx = max(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();
}
mx = max(mx,dp[1][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 |
80 ms |
43160 KB |
Output is correct |
2 |
Correct |
92 ms |
46472 KB |
Output is correct |
3 |
Correct |
34 ms |
36352 KB |
Output is correct |
4 |
Correct |
35 ms |
36172 KB |
Output is correct |
5 |
Correct |
240 ms |
74660 KB |
Output is correct |
6 |
Correct |
278 ms |
73924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
145 ms |
50668 KB |
Output is correct |
3 |
Correct |
165 ms |
54284 KB |
Output is correct |
4 |
Correct |
82 ms |
43200 KB |
Output is correct |
5 |
Correct |
95 ms |
46468 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23780 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
36 ms |
36296 KB |
Output is correct |
11 |
Correct |
34 ms |
36292 KB |
Output is correct |
12 |
Correct |
86 ms |
45864 KB |
Output is correct |
13 |
Correct |
100 ms |
49624 KB |
Output is correct |
14 |
Correct |
83 ms |
44580 KB |
Output is correct |
15 |
Correct |
94 ms |
44632 KB |
Output is correct |
16 |
Correct |
84 ms |
44572 KB |
Output is correct |
17 |
Correct |
91 ms |
46776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
36272 KB |
Output is correct |
2 |
Correct |
35 ms |
36288 KB |
Output is correct |
3 |
Correct |
64 ms |
38836 KB |
Output is correct |
4 |
Correct |
61 ms |
38684 KB |
Output is correct |
5 |
Correct |
93 ms |
43320 KB |
Output is correct |
6 |
Correct |
90 ms |
43344 KB |
Output is correct |
7 |
Correct |
97 ms |
43236 KB |
Output is correct |
8 |
Correct |
104 ms |
43280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23672 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23676 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23792 KB |
Output is correct |
10 |
Incorrect |
14 ms |
24020 KB |
1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23672 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23676 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23792 KB |
Output is correct |
10 |
Incorrect |
14 ms |
24020 KB |
1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23672 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23676 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23792 KB |
Output is correct |
10 |
Incorrect |
14 ms |
24020 KB |
1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
36272 KB |
Output is correct |
2 |
Correct |
35 ms |
36288 KB |
Output is correct |
3 |
Correct |
64 ms |
38836 KB |
Output is correct |
4 |
Correct |
61 ms |
38684 KB |
Output is correct |
5 |
Correct |
93 ms |
43320 KB |
Output is correct |
6 |
Correct |
90 ms |
43344 KB |
Output is correct |
7 |
Correct |
97 ms |
43236 KB |
Output is correct |
8 |
Correct |
104 ms |
43280 KB |
Output is correct |
9 |
Correct |
121 ms |
48008 KB |
Output is correct |
10 |
Incorrect |
75 ms |
36216 KB |
1st lines differ - on the 1st token, expected: '36454348383152', found: '36364983059693' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
43160 KB |
Output is correct |
2 |
Correct |
92 ms |
46472 KB |
Output is correct |
3 |
Correct |
34 ms |
36352 KB |
Output is correct |
4 |
Correct |
35 ms |
36172 KB |
Output is correct |
5 |
Correct |
240 ms |
74660 KB |
Output is correct |
6 |
Correct |
278 ms |
73924 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
145 ms |
50668 KB |
Output is correct |
9 |
Correct |
165 ms |
54284 KB |
Output is correct |
10 |
Correct |
82 ms |
43200 KB |
Output is correct |
11 |
Correct |
95 ms |
46468 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23780 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
36 ms |
36296 KB |
Output is correct |
17 |
Correct |
34 ms |
36292 KB |
Output is correct |
18 |
Correct |
86 ms |
45864 KB |
Output is correct |
19 |
Correct |
100 ms |
49624 KB |
Output is correct |
20 |
Correct |
83 ms |
44580 KB |
Output is correct |
21 |
Correct |
94 ms |
44632 KB |
Output is correct |
22 |
Correct |
84 ms |
44572 KB |
Output is correct |
23 |
Correct |
91 ms |
46776 KB |
Output is correct |
24 |
Correct |
33 ms |
36272 KB |
Output is correct |
25 |
Correct |
35 ms |
36288 KB |
Output is correct |
26 |
Correct |
64 ms |
38836 KB |
Output is correct |
27 |
Correct |
61 ms |
38684 KB |
Output is correct |
28 |
Correct |
93 ms |
43320 KB |
Output is correct |
29 |
Correct |
90 ms |
43344 KB |
Output is correct |
30 |
Correct |
97 ms |
43236 KB |
Output is correct |
31 |
Correct |
104 ms |
43280 KB |
Output is correct |
32 |
Correct |
12 ms |
23764 KB |
Output is correct |
33 |
Correct |
11 ms |
23672 KB |
Output is correct |
34 |
Correct |
12 ms |
23764 KB |
Output is correct |
35 |
Correct |
15 ms |
23780 KB |
Output is correct |
36 |
Correct |
12 ms |
23764 KB |
Output is correct |
37 |
Correct |
12 ms |
23676 KB |
Output is correct |
38 |
Correct |
13 ms |
23764 KB |
Output is correct |
39 |
Correct |
13 ms |
23764 KB |
Output is correct |
40 |
Correct |
12 ms |
23792 KB |
Output is correct |
41 |
Incorrect |
14 ms |
24020 KB |
1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912' |
42 |
Halted |
0 ms |
0 KB |
- |