# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
745389 |
2023-05-20T00:21:29 Z |
CSQ31 |
Catfish Farm (IOI22_fish) |
C++17 |
|
303 ms |
81016 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;
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().fi;
f.pop_back();
}
mx = max(mx,dp[1][i-1][ptr] + sum);
}
chmax(dp[1][i][j],mx - pref[i][j]);
}
if(i==1)continue;
//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
sum = pref[i-1].back();
mx = -INF;
ptr0 = sz(crd[i-2]);
ptr1 = sz(fish[i-1]) - 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 && fish[i-1][ptr1].fi > crd[i-2][ptr0]){
sum-=fish[i-1][ptr1].se;
ptr1--;
}
mx = max(mx,dp[0][i-2][ptr0] + sum);
mx = max(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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
44040 KB |
Output is correct |
2 |
Correct |
97 ms |
47452 KB |
Output is correct |
3 |
Correct |
33 ms |
36196 KB |
Output is correct |
4 |
Correct |
38 ms |
36228 KB |
Output is correct |
5 |
Correct |
261 ms |
81016 KB |
Output is correct |
6 |
Correct |
303 ms |
80792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
148 ms |
52832 KB |
Output is correct |
3 |
Correct |
172 ms |
57828 KB |
Output is correct |
4 |
Correct |
85 ms |
44036 KB |
Output is correct |
5 |
Correct |
101 ms |
47496 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
33 ms |
36264 KB |
Output is correct |
11 |
Correct |
33 ms |
36304 KB |
Output is correct |
12 |
Correct |
90 ms |
47240 KB |
Output is correct |
13 |
Correct |
103 ms |
51388 KB |
Output is correct |
14 |
Correct |
86 ms |
45648 KB |
Output is correct |
15 |
Correct |
97 ms |
46140 KB |
Output is correct |
16 |
Correct |
88 ms |
45756 KB |
Output is correct |
17 |
Correct |
96 ms |
48036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
36172 KB |
Output is correct |
2 |
Correct |
33 ms |
36308 KB |
Output is correct |
3 |
Correct |
61 ms |
39768 KB |
Output is correct |
4 |
Correct |
54 ms |
39308 KB |
Output is correct |
5 |
Correct |
88 ms |
44876 KB |
Output is correct |
6 |
Correct |
107 ms |
44268 KB |
Output is correct |
7 |
Correct |
92 ms |
44796 KB |
Output is correct |
8 |
Correct |
99 ms |
44920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23752 KB |
Output is correct |
2 |
Correct |
12 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23672 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23792 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23784 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23752 KB |
Output is correct |
2 |
Correct |
12 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23672 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23792 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23784 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23752 KB |
Output is correct |
2 |
Correct |
12 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23672 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23792 KB |
Output is correct |
8 |
Incorrect |
13 ms |
23784 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
36172 KB |
Output is correct |
2 |
Correct |
33 ms |
36308 KB |
Output is correct |
3 |
Correct |
61 ms |
39768 KB |
Output is correct |
4 |
Correct |
54 ms |
39308 KB |
Output is correct |
5 |
Correct |
88 ms |
44876 KB |
Output is correct |
6 |
Correct |
107 ms |
44268 KB |
Output is correct |
7 |
Correct |
92 ms |
44796 KB |
Output is correct |
8 |
Correct |
99 ms |
44920 KB |
Output is correct |
9 |
Correct |
98 ms |
49400 KB |
Output is correct |
10 |
Incorrect |
87 ms |
37944 KB |
1st lines differ - on the 1st token, expected: '36454348383152', found: '36364983059693' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
44040 KB |
Output is correct |
2 |
Correct |
97 ms |
47452 KB |
Output is correct |
3 |
Correct |
33 ms |
36196 KB |
Output is correct |
4 |
Correct |
38 ms |
36228 KB |
Output is correct |
5 |
Correct |
261 ms |
81016 KB |
Output is correct |
6 |
Correct |
303 ms |
80792 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
148 ms |
52832 KB |
Output is correct |
9 |
Correct |
172 ms |
57828 KB |
Output is correct |
10 |
Correct |
85 ms |
44036 KB |
Output is correct |
11 |
Correct |
101 ms |
47496 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
33 ms |
36264 KB |
Output is correct |
17 |
Correct |
33 ms |
36304 KB |
Output is correct |
18 |
Correct |
90 ms |
47240 KB |
Output is correct |
19 |
Correct |
103 ms |
51388 KB |
Output is correct |
20 |
Correct |
86 ms |
45648 KB |
Output is correct |
21 |
Correct |
97 ms |
46140 KB |
Output is correct |
22 |
Correct |
88 ms |
45756 KB |
Output is correct |
23 |
Correct |
96 ms |
48036 KB |
Output is correct |
24 |
Correct |
35 ms |
36172 KB |
Output is correct |
25 |
Correct |
33 ms |
36308 KB |
Output is correct |
26 |
Correct |
61 ms |
39768 KB |
Output is correct |
27 |
Correct |
54 ms |
39308 KB |
Output is correct |
28 |
Correct |
88 ms |
44876 KB |
Output is correct |
29 |
Correct |
107 ms |
44268 KB |
Output is correct |
30 |
Correct |
92 ms |
44796 KB |
Output is correct |
31 |
Correct |
99 ms |
44920 KB |
Output is correct |
32 |
Correct |
12 ms |
23752 KB |
Output is correct |
33 |
Correct |
12 ms |
23760 KB |
Output is correct |
34 |
Correct |
12 ms |
23764 KB |
Output is correct |
35 |
Correct |
12 ms |
23672 KB |
Output is correct |
36 |
Correct |
13 ms |
23788 KB |
Output is correct |
37 |
Correct |
13 ms |
23764 KB |
Output is correct |
38 |
Correct |
12 ms |
23792 KB |
Output is correct |
39 |
Incorrect |
13 ms |
23784 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
40 |
Halted |
0 ms |
0 KB |
- |