#include<bits/stdc++.h>
using namespace std;
//#include "fish.h"
typedef long long ll;
#pragma optimize("O3")
#define x first
#define y second
vector<vector<pair<ll,ll>>> cad;
vector<vector<ll>> indice;
/// @brief costo en la columna i
/// @param i columna
/// @param j valor del puente
/// @return
ll cost(ll i, ll j){
ll a=-1,b=cad[i].size();
while(abs(a-b)!=1){
ll m=(a+b)/2;
if(cad[i][m].x<=j) a=m;
else b=m;
}
if(b==0) return 0;
return cad[i][b-1].y;
}
/// @brief mayor k tal que indice[i][k] <= j
/// @param i columna
/// @param j valor
/// @return posicion
ll idx(ll i, ll j){
ll a=-1,b=indice[i].size();
while(abs(a-b)!=1){
ll m=(a+b)/2;
if(indice[i][m]<=j) a=m;
else b=m;
}
return b-1;
}
long long max_weights(int n, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> w) {
cad.resize(n+1); indice.resize(n+1);
vector<vector<vector<ll>>> dp(n+1);
for(int i=0; i<X.size(); i++){
cad[X[i]+1].push_back({Y[i]+1,w[i]});
if(X[i]!=0) indice[X[i]].push_back(Y[i]+1);
if(X[i]+2<=n) indice[X[i]+2].push_back(Y[i]+1);
}
for(int i=0; i<cad.size(); i++)
{
indice[i].push_back(0);
sort(cad[i].begin(),cad[i].end());
sort(indice[i].begin(),indice[i].end());
dp[i].assign(indice[i].size(),{0,0});
for(int j=1; j<cad[i].size(); j++)
cad[i][j].y+=cad[i][j-1].y;
}
vector<vector<vector<ll>>> dpac=dp;
ll bs=0;
for(int i=1; i<=n; i++)
{
for(int j=0; j<indice[i].size(); j++)
{
ll idxtemp=idx(i-1,indice[i][j]);
ll costoij=cost(i,indice[i][j]);
ll costoi_1j=cost(i-1,indice[i][j]);
if(idxtemp+1<dpac[i-1].size()) dp[i][j][0]=max(dp[i][j][0],dpac[i-1][idxtemp+1][0]-costoij);
if(idxtemp>-1) dp[i][j][1]=max(dp[i][j][1],dp[i-1][idxtemp][1]+costoi_1j);
if(i>=2)
{
dp[i][j][1]=max(dp[i][j][1],dpac[i-2].back()[0]);
dp[i][j][1]=max(dp[i][j][1],dpac[i-2][0][1]+costoi_1j);
}
dpac[i][j][0]=max(dp[i][j][0],dp[i][j][1]);
if(i<n) dpac[i][j][0]+=cost(i+1,indice[i][j]);
dpac[i][j][1]=max(dp[i][j][0],dp[i][j][1]);
bs=max(bs,max(dp[i][j][0],dp[i][j][1]));// salvar la respuesta
dp[i][j][1]-=costoij;
if(j>0)dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1]);
}
for(int j=indice[i].size()-2; j>-1; j--)
{
dpac[i][j][0]=max(dpac[i][j][0],dpac[i][j+1][0]);
dpac[i][j][1]=max(dpac[i][j][1],dpac[i][j+1][1]);
}
}
return bs;
}
Compilation message
fish.cpp:5: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
5 | #pragma optimize("O3")
|
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:40:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0; i<X.size(); i++){
| ~^~~~~~~~~
fish.cpp:45:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0; i<cad.size(); i++)
| ~^~~~~~~~~~~
fish.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j=1; j<cad[i].size(); j++)
| ~^~~~~~~~~~~~~~
fish.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int j=0; j<indice[i].size(); j++)
| ~^~~~~~~~~~~~~~~~~
fish.cpp:63:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if(idxtemp+1<dpac[i-1].size()) dp[i][j][0]=max(dp[i][j][0],dpac[i-1][idxtemp+1][0]-costoij);
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
35596 KB |
Output is correct |
2 |
Correct |
75 ms |
41144 KB |
Output is correct |
3 |
Correct |
27 ms |
25436 KB |
Output is correct |
4 |
Correct |
31 ms |
25432 KB |
Output is correct |
5 |
Correct |
235 ms |
108264 KB |
Output is correct |
6 |
Correct |
258 ms |
111956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
144 ms |
58252 KB |
Output is correct |
3 |
Correct |
172 ms |
72144 KB |
Output is correct |
4 |
Correct |
67 ms |
37056 KB |
Output is correct |
5 |
Correct |
76 ms |
42688 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
10 |
Correct |
26 ms |
25476 KB |
Output is correct |
11 |
Correct |
27 ms |
25432 KB |
Output is correct |
12 |
Correct |
90 ms |
46604 KB |
Output is correct |
13 |
Correct |
105 ms |
54468 KB |
Output is correct |
14 |
Correct |
79 ms |
41828 KB |
Output is correct |
15 |
Correct |
89 ms |
46528 KB |
Output is correct |
16 |
Correct |
81 ms |
42040 KB |
Output is correct |
17 |
Correct |
87 ms |
46560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
25436 KB |
Output is correct |
2 |
Correct |
28 ms |
25436 KB |
Output is correct |
3 |
Correct |
57 ms |
38488 KB |
Output is correct |
4 |
Correct |
52 ms |
35932 KB |
Output is correct |
5 |
Correct |
105 ms |
54228 KB |
Output is correct |
6 |
Correct |
106 ms |
54352 KB |
Output is correct |
7 |
Correct |
96 ms |
54368 KB |
Output is correct |
8 |
Correct |
101 ms |
54356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
856 KB |
Output is correct |
17 |
Correct |
31 ms |
13056 KB |
Output is correct |
18 |
Correct |
36 ms |
13916 KB |
Output is correct |
19 |
Correct |
30 ms |
13656 KB |
Output is correct |
20 |
Correct |
31 ms |
13656 KB |
Output is correct |
21 |
Correct |
31 ms |
13756 KB |
Output is correct |
22 |
Correct |
64 ms |
27044 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
21 ms |
9348 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
7 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
856 KB |
Output is correct |
17 |
Correct |
31 ms |
13056 KB |
Output is correct |
18 |
Correct |
36 ms |
13916 KB |
Output is correct |
19 |
Correct |
30 ms |
13656 KB |
Output is correct |
20 |
Correct |
31 ms |
13656 KB |
Output is correct |
21 |
Correct |
31 ms |
13756 KB |
Output is correct |
22 |
Correct |
64 ms |
27044 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
21 ms |
9348 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
7 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
1884 KB |
Output is correct |
28 |
Correct |
177 ms |
66820 KB |
Output is correct |
29 |
Correct |
266 ms |
92264 KB |
Output is correct |
30 |
Correct |
222 ms |
91472 KB |
Output is correct |
31 |
Correct |
226 ms |
91628 KB |
Output is correct |
32 |
Correct |
224 ms |
93520 KB |
Output is correct |
33 |
Correct |
218 ms |
91448 KB |
Output is correct |
34 |
Correct |
220 ms |
91472 KB |
Output is correct |
35 |
Correct |
82 ms |
36796 KB |
Output is correct |
36 |
Correct |
211 ms |
94548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
25436 KB |
Output is correct |
2 |
Correct |
28 ms |
25436 KB |
Output is correct |
3 |
Correct |
57 ms |
38488 KB |
Output is correct |
4 |
Correct |
52 ms |
35932 KB |
Output is correct |
5 |
Correct |
105 ms |
54228 KB |
Output is correct |
6 |
Correct |
106 ms |
54352 KB |
Output is correct |
7 |
Correct |
96 ms |
54368 KB |
Output is correct |
8 |
Correct |
101 ms |
54356 KB |
Output is correct |
9 |
Correct |
101 ms |
54396 KB |
Output is correct |
10 |
Correct |
85 ms |
42576 KB |
Output is correct |
11 |
Correct |
188 ms |
84564 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
27 ms |
25288 KB |
Output is correct |
19 |
Correct |
27 ms |
25304 KB |
Output is correct |
20 |
Correct |
28 ms |
25432 KB |
Output is correct |
21 |
Correct |
27 ms |
25432 KB |
Output is correct |
22 |
Correct |
121 ms |
54228 KB |
Output is correct |
23 |
Correct |
191 ms |
84560 KB |
Output is correct |
24 |
Correct |
192 ms |
84740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
35596 KB |
Output is correct |
2 |
Correct |
75 ms |
41144 KB |
Output is correct |
3 |
Correct |
27 ms |
25436 KB |
Output is correct |
4 |
Correct |
31 ms |
25432 KB |
Output is correct |
5 |
Correct |
235 ms |
108264 KB |
Output is correct |
6 |
Correct |
258 ms |
111956 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
144 ms |
58252 KB |
Output is correct |
9 |
Correct |
172 ms |
72144 KB |
Output is correct |
10 |
Correct |
67 ms |
37056 KB |
Output is correct |
11 |
Correct |
76 ms |
42688 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
432 KB |
Output is correct |
16 |
Correct |
26 ms |
25476 KB |
Output is correct |
17 |
Correct |
27 ms |
25432 KB |
Output is correct |
18 |
Correct |
90 ms |
46604 KB |
Output is correct |
19 |
Correct |
105 ms |
54468 KB |
Output is correct |
20 |
Correct |
79 ms |
41828 KB |
Output is correct |
21 |
Correct |
89 ms |
46528 KB |
Output is correct |
22 |
Correct |
81 ms |
42040 KB |
Output is correct |
23 |
Correct |
87 ms |
46560 KB |
Output is correct |
24 |
Correct |
28 ms |
25436 KB |
Output is correct |
25 |
Correct |
28 ms |
25436 KB |
Output is correct |
26 |
Correct |
57 ms |
38488 KB |
Output is correct |
27 |
Correct |
52 ms |
35932 KB |
Output is correct |
28 |
Correct |
105 ms |
54228 KB |
Output is correct |
29 |
Correct |
106 ms |
54352 KB |
Output is correct |
30 |
Correct |
96 ms |
54368 KB |
Output is correct |
31 |
Correct |
101 ms |
54356 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
1116 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
2 ms |
856 KB |
Output is correct |
48 |
Correct |
31 ms |
13056 KB |
Output is correct |
49 |
Correct |
36 ms |
13916 KB |
Output is correct |
50 |
Correct |
30 ms |
13656 KB |
Output is correct |
51 |
Correct |
31 ms |
13656 KB |
Output is correct |
52 |
Correct |
31 ms |
13756 KB |
Output is correct |
53 |
Correct |
64 ms |
27044 KB |
Output is correct |
54 |
Correct |
7 ms |
3164 KB |
Output is correct |
55 |
Correct |
21 ms |
9348 KB |
Output is correct |
56 |
Correct |
1 ms |
860 KB |
Output is correct |
57 |
Correct |
7 ms |
2908 KB |
Output is correct |
58 |
Correct |
3 ms |
1884 KB |
Output is correct |
59 |
Correct |
177 ms |
66820 KB |
Output is correct |
60 |
Correct |
266 ms |
92264 KB |
Output is correct |
61 |
Correct |
222 ms |
91472 KB |
Output is correct |
62 |
Correct |
226 ms |
91628 KB |
Output is correct |
63 |
Correct |
224 ms |
93520 KB |
Output is correct |
64 |
Correct |
218 ms |
91448 KB |
Output is correct |
65 |
Correct |
220 ms |
91472 KB |
Output is correct |
66 |
Correct |
82 ms |
36796 KB |
Output is correct |
67 |
Correct |
211 ms |
94548 KB |
Output is correct |
68 |
Correct |
101 ms |
54396 KB |
Output is correct |
69 |
Correct |
85 ms |
42576 KB |
Output is correct |
70 |
Correct |
188 ms |
84564 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
27 ms |
25288 KB |
Output is correct |
78 |
Correct |
27 ms |
25304 KB |
Output is correct |
79 |
Correct |
28 ms |
25432 KB |
Output is correct |
80 |
Correct |
27 ms |
25432 KB |
Output is correct |
81 |
Correct |
121 ms |
54228 KB |
Output is correct |
82 |
Correct |
191 ms |
84560 KB |
Output is correct |
83 |
Correct |
192 ms |
84740 KB |
Output is correct |
84 |
Correct |
281 ms |
112048 KB |
Output is correct |
85 |
Correct |
293 ms |
113828 KB |
Output is correct |
86 |
Correct |
265 ms |
115540 KB |
Output is correct |
87 |
Correct |
296 ms |
118760 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
274 ms |
118888 KB |
Output is correct |
90 |
Correct |
271 ms |
117324 KB |
Output is correct |
91 |
Correct |
242 ms |
117092 KB |
Output is correct |