Submission #815543

#TimeUsernameProblemLanguageResultExecution timeMemory
815543ZeroCoolExam (eJOI20_exam)C++14
100 / 100
331 ms221908 KiB
#include <bits/stdc++.h> #define INF 1e18 #define LL long long #define int long long using namespace std; const int mod = 1e9 + 7; const int inf = 1e18; const int mxn = 1e5 +5; struct FenwickTree{ vector<int> bit; int n; FenwickTree(int _n){ n = _n + 69; bit.resize(n,0); } void update(int pos,int val){ for(pos++;pos<n;pos += pos & -pos)bit[pos] = max(bit[pos],val); } int query(int pos){ int ans = 0; for(pos++;pos;pos -= pos & -pos)ans = max(bit[pos],ans); return ans; } }; int n; int A[mxn]; int B[mxn]; int l[mxn]; int r[mxn]; void two_pointers(){ int x = B[0]; int res = 0; for(int i = 0;i<n;i++) { if(A[i] == x) { res++; int j = i - 1; while(j >= 0 && A[j] <= x) { j--; res++; } j = i + 1; while(j < n && A[j] <= x) { res++; j++; } i = j - 1; } } cout<<res<<endl; } int dp[5005][5005]; bool vis[5005][5005]; int f(int i,int j){ if(i == -1 ||j == -1)return 0; if(vis[i][j])return dp[i][j]; int ans = 0; if(A[i] == B[j] && l[i] < j && r[i] > j){ ans = max(ans, f(i,j-1) + 1); } ans = max(ans , f(i-1,j)); ans = max(ans, f(i,j-1)); vis[i][j] = true; dp[i][j] = ans; return ans; } void solve(){ cin>>n; set<int>s; int c[n+5]; for(int i = 0;i<n;i++)cin>>A[i]; for(int i = 0;i<n;i++){ cin>>B[i]; s.insert(B[i]); } for(int i = 0;i<n;i++)c[i+1] = A[i]; c[0] = inf; c[n+1] = inf; stack<int> st; st.push(0); for(int i = 1;i<=n;i++){ while(st.size() && (c[st.top()] <= c[i]))st.pop(); l[i-1] = st.top() - 1; st.push(i); } //st = stack<int>(); while(st.size())st.pop(); st.push(n+1); for(int i = n;i;i--){ while(st.size() && (c[st.top()] <= c[i]))st.pop(); //cout<<"tukaa"<<endl; r[i-1] = st.top() - 1; //if(r[i-1] >= n)r[i-1] = -1; st.push(i); } bool sub2 = true; for(int i = 1;i<n;i++){ if(B[i] != B[0]){ sub2 = false; break; } } if(sub2){ two_pointers(); return; } if(n <= 5000){ cout<<f(n-1,n-1)<<endl; return; } map<int,int> mp; for(int i = 0;i<n;i++){ mp[A[i]] = i; } FenwickTree fwt(n); for(int i = 0;i<n;i++){ auto it = mp.find(B[i]); if(it != mp.end()){ int idx = it->second; if(l[idx] < i and i < r[idx]) { int dp = fwt.query(idx); fwt.update(idx, dp + 1); } } } cout<<fwt.query(n-1)<<endl; } int32_t main() { #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(0); #endif solve(); }
#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...