제출 #928881

#제출 시각아이디문제언어결과실행 시간메모리
928881yellowtoadFinancial Report (JOI21_financial)C++17
100 / 100
489 ms31940 KiB
#include <iostream> #include <vector> #include <algorithm> #include <set> #define f first #define s second using namespace std; int n, m, dp[300010]; pair<int,int> node[1200010]; vector<pair<int,int>> v; set<int> bst; set<int>::iterator iter; bool done; void update(int id, int x, int y, int pos, pair<int,int> val) { if (x == y) { node[id] = val; return; } int mid = (x+y)/2; if (pos <= mid) update(id*2,x,mid,pos,val); else update(id*2+1,mid+1,y,pos,val); node[id] = {max(node[id*2].f,node[id*2+1].f),max(node[id*2].s,node[id*2+1].s)}; } int findll(int id, int x, int y, int r) { if (r < x) return r+1; if (node[id].s <= m) return x-1; if (x == y) { done = 1; return x; } int mid = (x+y)/2, tmp = findll(id*2+1,mid+1,y,r); if (!done) return min(tmp,findll(id*2,x,mid,r)); else return tmp; } int findl(int r) { done = 0; return findll(1,1,n+1,r); } int find(int id, int x, int y, int l, int r) { if ((l <= x) && (y <= r)) return node[id].f; if ((y < l) || (r < x)) return 0; int mid = (x+y)/2; return max(find(id*2,x,mid,l,r),find(id*2+1,mid+1,y,l,r)); } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { int x; cin >> x; v.push_back({x,-i}); } sort(v.begin(),v.end()); for (int i = 0; i < n; i++) v[i].s = -v[i].s; update(1,1,n+1,n+1,{0,n+1}); bst.insert(n+1); bst.insert(0); for (int i = 0; i < n; i++) { iter = bst.upper_bound(v[i].s); if (v[i].s-(*prev(iter)) <= m) dp[v[i].s] = find(1,1,n+1,max(1,findl(v[i].s)-m),v[i].s)+1; else dp[v[i].s] = find(1,1,n+1,max(1,v[i].s-m),v[i].s)+1; update(1,1,n+1,*iter,{dp[*iter],(*iter)-v[i].s}); update(1,1,n+1,v[i].s,{dp[v[i].s],v[i].s-(*prev(iter))}); //cout << *iter << " " << *prev(iter) << endl; bst.insert(v[i].s); } cout << node[1].f << endl; }
#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...