Submission #1127396

#TimeUsernameProblemLanguageResultExecution timeMemory
1127396khanhphucscratchExam (eJOI20_exam)C++20
0 / 100
906 ms98268 KiB
//n <= 5000, subtask 1, 3, 5, 6 #include<bits/stdc++.h> using namespace std; int dp[5005][5005], a[5005], b[5005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; for(int i = 1; i <= n; i++) cin>>a[i]; for(int i = 1; i <= n; i++) cin>>b[i]; int curmaxa = 0; for(int i = 1; i <= n; i++){ curmaxa = max(curmaxa, a[i]); if(a[i] == curmaxa) dp[0][i] = 0; else dp[0][i] = -1e9; } for(int i = 1; i <= n; i++){ //Previous is current max of now stack<int> value_stack, dp_stack; for(int j = 1; j <= n; j++){ while(value_stack.size() > 0 && a[value_stack.top()] < a[j]){ int x = value_stack.top(); value_stack.pop(); //Update dp stack if(dp_stack.size() > 0 && dp_stack.top() == x) dp_stack.pop(); } value_stack.push(j); if(dp_stack.size() == 0 || dp[i-1][dp_stack.top()] < dp[i-1][j]) dp_stack.push(j); dp[i][j] = max(dp[i][j], dp[i-1][dp_stack.top()]); } //Now is the current max of previous stack<pair<int, int>> dp_val_stack; for(int j = 1; j <= n; j++){ int curmax = dp[i-1][j]; while(dp_val_stack.size() > 0 && a[dp_val_stack.top().first] <= a[j]){ curmax = max(curmax, dp_val_stack.top().second); dp_val_stack.pop(); } dp_val_stack.push({j, curmax}); dp[i][j] = max(dp[i][j], curmax); } //Update b for(int j = 1; j <= n; j++) if(b[i] == a[j]) dp[i][j]++; //for(int j = 1; j <= n; j++) cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; } int ans = 0, curmax = 0; for(int i = n; i >= 1; i--){ curmax = max(curmax, a[i]); if(a[i] == curmax) ans = max(ans, dp[n][i]); } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...