#include"fish.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<limits.h>
using namespace std;
typedef long long ll;
int n;
vector<pair<int,int>>fish[100001];
vector<ll>sum_down[100001];
vector<ll>sum_right[100001];
vector<ll>sum_left[100001];
vector<ll>next_right[100001];
vector<ll>next_left[100001];
int h[100001];
vector<ll>dp1[1000001];
vector<bool>dpt1[1000001];
vector<ll>dp2[1000001];
vector<bool>dpt2[1000001];
vector<ll>dp3[1000001];
vector<bool>dpt3[1000001];
ll dp4[1000001];
bool dpt4[1000001];
ll get_slope_down(int x,int y);
ll get_slope_up(int x,int y);
ll get_gap(int x,ll min_sum);
ll get_full(int x);
ll get1(int y2,int x){
if(y2<0)
return LLONG_MIN;
ll res=get_slope_down(x+1,y2);
res=max(res,get1(y2-1,x));
return res;
}
ll get_slope_down(int x,int y){
if(x==n-1){
return 0;
}
if(dpt1[x][y])
return dp1[x][y];
ll res=LLONG_MIN;
for(int y2=0;y2<next_right[x][y];y2++){
ll res2=get_slope_down(x+1,y2)-sum_down[x][y]+sum_right[x][y];
res=max(res,res2);
}
ll res2=get_gap(x+1,sum_right[x][y])-sum_down[x][y];
res=max(res,res2);
dpt1[x][y]=true;
dp1[x][y]=res;
return res;
}
ll get2(int y2,int x){
if(y2<0)
return LLONG_MIN;
ll res=get_slope_up(x+1,y2);
res=max(res,get2(y2-1,x));
return res;
}
ll get_slope_up(int x,int y){
if(x==n-1){
return 0;
}
if(dpt2[x][y])
return dp2[x][y];
ll res=LLONG_MIN;
for(int y2=0;y2<h[x+1];y2++){
if(fish[x][y].first<=fish[x][y2].first){
ll res2=get_slope_up(x+1,y2)-sum_down[x][y]+sum_left[x][y];
res=max(res,res2);
}
}
ll res2=get_full(x+1)+sum_left[x+1].back()-sum_down[x][y]+sum_left[x][y];
res=max(res,res2);
dpt2[x][y]=true;
dp2[x][y]=res;
return res;
}
ll get_gap(int x,ll min_sum){
if(x==n-1)
return min_sum;
ll res=0;
res=max(res,get_full(x+1)+sum_left[x+1].back());
for(int y2=0;y2<h[x+1];y2++){
ll res2=get_slope_up(x+1,y2)+max(sum_left[x+1][y2],min_sum)-sum_left[x+1][y2];
res=max(res,res2);
}
return res;
}
ll get_full(int x){
if(x==n-1)
return 0;
if(dpt4[x])
return dp4[x];
ll res=0;
res=max(res,get_gap(x+1,sum_right[x].back()));
for(int y2=0;y2<h[x+1];y2++){
ll res2=get_slope_down(x+1,y2)+sum_right[x].back();
res=max(res,res2);
}
dpt4[x]=true;
dp4[x]=res;
return res;
}
ll max_weights(int N,int M,vector<int>X,vector<int>Y,vector<int>W){
n=N+1;
for(int i=0;i<M;i++)
fish[X[i]+1].push_back({Y[i],W[i]});
for(int i=0;i<n;i++)
sort(fish[i].begin(),fish[i].end());
for(int i=0;i<n;i++)
fish[i].push_back({n,0});
for(int i=0;i<n;i++)
h[i]=(int)fish[i].size();
for(int i=0;i<n;i++){
sum_down[i].resize(h[i]);
for(int j=1;j<h[i];j++){
sum_down[i][j]=sum_down[i][j-1]+fish[i][j-1].second;
}
}
for(int i=0;i<n;i++){
dp1[i].resize(h[i]);
dpt1[i].resize(h[i]);
dp2[i].resize(h[i]);
dpt2[i].resize(h[i]);
dp3[i].resize(h[i]);
dpt3[i].resize(h[i]);
}
sum_right[n-1].resize(h[n-1]);
for(int i=0;i<n-1;i++){
sum_right[i].resize(h[i]);
next_right[i].resize(h[i]);
int i2=0;
ll sum=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i+1]&&fish[i][i1].first>fish[i+1][i2].first){
sum+=fish[i+1][i2].second;
i2++;
}
sum_right[i][i1]=sum;
}
i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i+1]&&fish[i][i1].first>=fish[i+1][i2].first)
i2++;
next_right[i][i1]=i2;
}
}
sum_left[0].resize(h[0]);
for(int i=1;i<n;i++){
sum_left[i].resize(h[i]);
next_left[i].resize(h[i]);
int i2=0;
ll sum=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i-1]&&fish[i][i1].first>fish[i-1][i2].first){
sum+=fish[i-1][i2].second;
i2++;
}
sum_left[i][i1]=sum;
}
i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i-1]&&fish[i][i1].first>=fish[i-1][i2].first)
i2++;
next_left[i][i1]=i2;
}
}
return get_gap(0,0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
251772 KB |
Output is correct |
2 |
Correct |
183 ms |
258272 KB |
Output is correct |
3 |
Correct |
152 ms |
248920 KB |
Output is correct |
4 |
Correct |
157 ms |
248896 KB |
Output is correct |
5 |
Correct |
274 ms |
277164 KB |
Output is correct |
6 |
Correct |
370 ms |
273164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
202260 KB |
Output is correct |
2 |
Execution timed out |
1104 ms |
259536 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
248924 KB |
Output is correct |
2 |
Correct |
155 ms |
248872 KB |
Output is correct |
3 |
Correct |
181 ms |
245520 KB |
Output is correct |
4 |
Correct |
181 ms |
249772 KB |
Output is correct |
5 |
Correct |
210 ms |
251228 KB |
Output is correct |
6 |
Correct |
209 ms |
251252 KB |
Output is correct |
7 |
Correct |
212 ms |
251204 KB |
Output is correct |
8 |
Correct |
209 ms |
251180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
202160 KB |
Output is correct |
2 |
Correct |
90 ms |
202192 KB |
Output is correct |
3 |
Correct |
90 ms |
202224 KB |
Output is correct |
4 |
Correct |
91 ms |
202256 KB |
Output is correct |
5 |
Correct |
91 ms |
202136 KB |
Output is correct |
6 |
Correct |
90 ms |
202164 KB |
Output is correct |
7 |
Correct |
93 ms |
202272 KB |
Output is correct |
8 |
Correct |
94 ms |
202216 KB |
Output is correct |
9 |
Correct |
99 ms |
202356 KB |
Output is correct |
10 |
Correct |
92 ms |
202572 KB |
Output is correct |
11 |
Correct |
89 ms |
202292 KB |
Output is correct |
12 |
Correct |
94 ms |
202576 KB |
Output is correct |
13 |
Correct |
93 ms |
202188 KB |
Output is correct |
14 |
Correct |
91 ms |
202464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
202160 KB |
Output is correct |
2 |
Correct |
90 ms |
202192 KB |
Output is correct |
3 |
Correct |
90 ms |
202224 KB |
Output is correct |
4 |
Correct |
91 ms |
202256 KB |
Output is correct |
5 |
Correct |
91 ms |
202136 KB |
Output is correct |
6 |
Correct |
90 ms |
202164 KB |
Output is correct |
7 |
Correct |
93 ms |
202272 KB |
Output is correct |
8 |
Correct |
94 ms |
202216 KB |
Output is correct |
9 |
Correct |
99 ms |
202356 KB |
Output is correct |
10 |
Correct |
92 ms |
202572 KB |
Output is correct |
11 |
Correct |
89 ms |
202292 KB |
Output is correct |
12 |
Correct |
94 ms |
202576 KB |
Output is correct |
13 |
Correct |
93 ms |
202188 KB |
Output is correct |
14 |
Correct |
91 ms |
202464 KB |
Output is correct |
15 |
Correct |
87 ms |
202412 KB |
Output is correct |
16 |
Correct |
93 ms |
202448 KB |
Output is correct |
17 |
Correct |
219 ms |
206736 KB |
Output is correct |
18 |
Correct |
228 ms |
206884 KB |
Output is correct |
19 |
Correct |
178 ms |
206892 KB |
Output is correct |
20 |
Correct |
185 ms |
206876 KB |
Output is correct |
21 |
Correct |
183 ms |
206884 KB |
Output is correct |
22 |
Correct |
408 ms |
211352 KB |
Output is correct |
23 |
Correct |
99 ms |
203248 KB |
Output is correct |
24 |
Correct |
132 ms |
205252 KB |
Output is correct |
25 |
Correct |
90 ms |
202392 KB |
Output is correct |
26 |
Correct |
94 ms |
203068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
202160 KB |
Output is correct |
2 |
Correct |
90 ms |
202192 KB |
Output is correct |
3 |
Correct |
90 ms |
202224 KB |
Output is correct |
4 |
Correct |
91 ms |
202256 KB |
Output is correct |
5 |
Correct |
91 ms |
202136 KB |
Output is correct |
6 |
Correct |
90 ms |
202164 KB |
Output is correct |
7 |
Correct |
93 ms |
202272 KB |
Output is correct |
8 |
Correct |
94 ms |
202216 KB |
Output is correct |
9 |
Correct |
99 ms |
202356 KB |
Output is correct |
10 |
Correct |
92 ms |
202572 KB |
Output is correct |
11 |
Correct |
89 ms |
202292 KB |
Output is correct |
12 |
Correct |
94 ms |
202576 KB |
Output is correct |
13 |
Correct |
93 ms |
202188 KB |
Output is correct |
14 |
Correct |
91 ms |
202464 KB |
Output is correct |
15 |
Correct |
87 ms |
202412 KB |
Output is correct |
16 |
Correct |
93 ms |
202448 KB |
Output is correct |
17 |
Correct |
219 ms |
206736 KB |
Output is correct |
18 |
Correct |
228 ms |
206884 KB |
Output is correct |
19 |
Correct |
178 ms |
206892 KB |
Output is correct |
20 |
Correct |
185 ms |
206876 KB |
Output is correct |
21 |
Correct |
183 ms |
206884 KB |
Output is correct |
22 |
Correct |
408 ms |
211352 KB |
Output is correct |
23 |
Correct |
99 ms |
203248 KB |
Output is correct |
24 |
Correct |
132 ms |
205252 KB |
Output is correct |
25 |
Correct |
90 ms |
202392 KB |
Output is correct |
26 |
Correct |
94 ms |
203068 KB |
Output is correct |
27 |
Correct |
91 ms |
203836 KB |
Output is correct |
28 |
Execution timed out |
1102 ms |
224172 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
248924 KB |
Output is correct |
2 |
Correct |
155 ms |
248872 KB |
Output is correct |
3 |
Correct |
181 ms |
245520 KB |
Output is correct |
4 |
Correct |
181 ms |
249772 KB |
Output is correct |
5 |
Correct |
210 ms |
251228 KB |
Output is correct |
6 |
Correct |
209 ms |
251252 KB |
Output is correct |
7 |
Correct |
212 ms |
251204 KB |
Output is correct |
8 |
Correct |
209 ms |
251180 KB |
Output is correct |
9 |
Correct |
215 ms |
251164 KB |
Output is correct |
10 |
Correct |
175 ms |
228972 KB |
Output is correct |
11 |
Correct |
259 ms |
255724 KB |
Output is correct |
12 |
Correct |
95 ms |
202248 KB |
Output is correct |
13 |
Correct |
88 ms |
202248 KB |
Output is correct |
14 |
Correct |
86 ms |
202368 KB |
Output is correct |
15 |
Correct |
85 ms |
202220 KB |
Output is correct |
16 |
Correct |
97 ms |
202136 KB |
Output is correct |
17 |
Correct |
93 ms |
202172 KB |
Output is correct |
18 |
Correct |
165 ms |
248796 KB |
Output is correct |
19 |
Correct |
157 ms |
248912 KB |
Output is correct |
20 |
Correct |
165 ms |
248884 KB |
Output is correct |
21 |
Correct |
163 ms |
248820 KB |
Output is correct |
22 |
Incorrect |
209 ms |
251748 KB |
1st lines differ - on the 1st token, expected: '45561826463480', found: '45551919437846' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
251772 KB |
Output is correct |
2 |
Correct |
183 ms |
258272 KB |
Output is correct |
3 |
Correct |
152 ms |
248920 KB |
Output is correct |
4 |
Correct |
157 ms |
248896 KB |
Output is correct |
5 |
Correct |
274 ms |
277164 KB |
Output is correct |
6 |
Correct |
370 ms |
273164 KB |
Output is correct |
7 |
Correct |
91 ms |
202260 KB |
Output is correct |
8 |
Execution timed out |
1104 ms |
259536 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |