#include <bits/stdc++.h>
using namespace std;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
typedef long long ll;
#include "fish.h"
vector<vector<ll>> dp;
vector<int> X,Y;
int N,M;
vector<vector<pair<int,pair<ll,int>>>> arr;
ll query(int x, int l, int r){
ll rval = 0;
for (int i = 0; i < arr[x].size(); i++){
if (arr[x][i].first>=l && arr[x][i].first<=r){
rval+=arr[x][i].second.first;
}
}
return rval;
}
int getup(int node){
int xv = X[node];
int yv = Y[node];
for (int i = 0; i < arr[xv].size(); i++){
if (arr[xv][i].first>yv){
return arr[xv][i].second.second;
}
}
assert(false);
return 23;
}
int getdown(int node){
int xv = X[node];
int yv = Y[node];
for (int i = arr[xv].size()-1; i >= 0; i--){
if (arr[xv][i].first<yv){
return arr[xv][i].second.second;
}
}
assert(false);
return 23;
}
int getnextup(int node){
int xv = X[node];
int yv = Y[node];
for (int i = 0; i < arr[xv+1].size(); i++){
if (arr[xv+1][i].first>=yv){
return arr[xv+1][i].second.second;
}
}
assert(false);
return 23;
}
int getnextdown(int node){
int xv = X[node];
int yv = Y[node];
for (int i = arr[xv+1].size()-1; i >= 0; i--){
if (arr[xv+1][i].first<=yv){
return arr[xv+1][i].second.second;
}
}
assert(false);
return 23;
}
long long f(int node, int flag){
int x = X[node];
int y = Y[node];
//cout<<x<<" "<<y<<" "<<flag<<endl;
if (dp[node][flag]!=-1) return dp[node][flag];
if (flag==1){
dp[node][flag]=0;
if (y<N){
ll crr = 0;
int next = getup(node);
if (x>0) crr = query(x-1,y,Y[next]-1);
dp[node][flag]=crr+f(getup(node),1);
}
if (x+1<N){
int next = getnextup(node);
ll crr = query(x,y,Y[next]-1);
dp[node][flag]=max(dp[node][flag],crr+f(next,1));
next=getnextdown(node);
crr = query(x+1,Y[next],y-1);
dp[node][flag]=max(dp[node][flag],crr+f(next,0));
}
}
else if (flag==0){
dp[node][flag]=0;
if (y-1>=0){
int next = getdown(node);
dp[node][flag]=query(x,Y[next],y-1)+f(next,0);
}
if (x+1<N){
dp[node][flag]=max(dp[node][flag],f(getnextup(node),0));
if (y==0){
dp[node][flag]=max(dp[node][flag],f(M+x+1,2));
}
}
}
else {
dp[node][flag]=0;
if (y<N){
dp[node][flag]=f(getup(node),2);
}
if (x+1<N){
int next = getnextup(node);
ll crr = query(x,y,Y[next]-1);
dp[node][flag]=max(dp[node][flag],crr+f(next,1));
next=getnextdown(node);
crr = query(x+1,Y[next],y-1);
dp[node][flag]=max(dp[node][flag],crr+f(next,0));
}
}
if (flag!=0 && x+2<N){
dp[node][flag]=max(dp[node][flag],f(M+x+2,1));
}
//cout<<x<<" "<<y<<" "<<flag<<" "<<dp[node][flag]<<endl;
return dp[node][flag];
};
long long max_weights(int _N, int _M, std::vector<int> _X, std::vector<int> _Y, std::vector<int> _W) {
N=_N;
vector<int> W;
arr.resize(N);
vector<int> bb(N,0);
for (int i = 0; i < _Y.size(); ++i)
{
if (_Y[i]==0) bb[_X[i]]=_W[i];
else {
X.push_back(_X[i]);
Y.push_back(_Y[i]);
W.push_back(_W[i]);
M++;
}
}
for (int i = 0; i < N; ++i)
{
X.push_back(i);
Y.push_back(0);
W.push_back(bb[i]);
}
for (int i = 0; i < N; i++){
X.push_back(i);
Y.push_back(N);
W.push_back(0);
}
for (int i = 0; i < Y.size(); i++){
arr[X[i]].push_back({Y[i],{W[i],i}});
}
for (int i = 0; i < N; ++i)
{
sort(arr[i].begin(), arr[i].end());
}
//0 decreasing
//1 increasing
//2 increasing, \
but forbidden to profit from back
//coutarr(X);
//coutarr(Y);
//coutarr(W);
//cout<<M<<endl;
dp.resize(Y.size(),vector<ll>(3,-1));
return f(M,2);
}
Compilation message
fish.cpp:154:5: warning: multi-line comment [-Wcomment]
154 | //2 increasing, \
| ^
fish.cpp: In function 'll query(int, int, int)':
fish.cpp:12:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int i = 0; i < arr[x].size(); i++){
| ~~^~~~~~~~~~~~~~~
fish.cpp: In function 'int getup(int)':
fish.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < arr[xv].size(); i++){
| ~~^~~~~~~~~~~~~~~~
fish.cpp: In function 'int getnextup(int)':
fish.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < arr[xv+1].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:124:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = 0; i < _Y.size(); ++i)
| ~~^~~~~~~~~~~
fish.cpp:145:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for (int i = 0; i < Y.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
34856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1085 ms |
43992 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
30436 KB |
Output is correct |
2 |
Correct |
46 ms |
30368 KB |
Output is correct |
3 |
Correct |
51 ms |
28656 KB |
Output is correct |
4 |
Correct |
52 ms |
31284 KB |
Output is correct |
5 |
Correct |
77 ms |
32684 KB |
Output is correct |
6 |
Correct |
62 ms |
32800 KB |
Output is correct |
7 |
Correct |
68 ms |
32828 KB |
Output is correct |
8 |
Correct |
64 ms |
32788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
98 ms |
5704 KB |
Output is correct |
18 |
Correct |
103 ms |
6476 KB |
Output is correct |
19 |
Correct |
92 ms |
6276 KB |
Output is correct |
20 |
Correct |
78 ms |
6280 KB |
Output is correct |
21 |
Correct |
80 ms |
6280 KB |
Output is correct |
22 |
Correct |
238 ms |
11836 KB |
Output is correct |
23 |
Correct |
8 ms |
1492 KB |
Output is correct |
24 |
Correct |
43 ms |
3924 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
98 ms |
5704 KB |
Output is correct |
18 |
Correct |
103 ms |
6476 KB |
Output is correct |
19 |
Correct |
92 ms |
6276 KB |
Output is correct |
20 |
Correct |
78 ms |
6280 KB |
Output is correct |
21 |
Correct |
80 ms |
6280 KB |
Output is correct |
22 |
Correct |
238 ms |
11836 KB |
Output is correct |
23 |
Correct |
8 ms |
1492 KB |
Output is correct |
24 |
Correct |
43 ms |
3924 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
1364 KB |
Output is correct |
27 |
Correct |
3 ms |
1620 KB |
Output is correct |
28 |
Execution timed out |
1070 ms |
27380 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
30436 KB |
Output is correct |
2 |
Correct |
46 ms |
30368 KB |
Output is correct |
3 |
Correct |
51 ms |
28656 KB |
Output is correct |
4 |
Correct |
52 ms |
31284 KB |
Output is correct |
5 |
Correct |
77 ms |
32684 KB |
Output is correct |
6 |
Correct |
62 ms |
32800 KB |
Output is correct |
7 |
Correct |
68 ms |
32828 KB |
Output is correct |
8 |
Correct |
64 ms |
32788 KB |
Output is correct |
9 |
Correct |
122 ms |
44296 KB |
Output is correct |
10 |
Correct |
66 ms |
23472 KB |
Output is correct |
11 |
Correct |
140 ms |
46652 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
42 ms |
30396 KB |
Output is correct |
19 |
Correct |
43 ms |
30404 KB |
Output is correct |
20 |
Correct |
42 ms |
30376 KB |
Output is correct |
21 |
Correct |
42 ms |
30396 KB |
Output is correct |
22 |
Correct |
114 ms |
42636 KB |
Output is correct |
23 |
Correct |
171 ms |
53216 KB |
Output is correct |
24 |
Correct |
195 ms |
53672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
34856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |