Submission #1262099

#TimeUsernameProblemLanguageResultExecution timeMemory
1262099nguynFinancial Report (JOI21_financial)C++20
100 / 100
275 ms18668 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define F first
#define S second
#define pb push_back 
#define pii pair<int,int>

const int N = 3e5 + 5;

struct SegTree {
	int n;
	vector<int> st;

	SegTree() {}
	SegTree(int n) : n(n) {
		st.resize(4 * n + 5); 
	}

	void update(int id, int l, int r, int i, int x) {
		if (l == r) {
			st[id] = x;
			return; 
		}
		int mid = (l + r) / 2;
		if (mid >= i) update(id * 2, l, mid, i, x);
		else update(id * 2 + 1, mid + 1, r, i, x); 
		st[id] = max(st[id * 2], st[id * 2 + 1]); 
	}

	int get(int id, int l, int r, int u, int v) {
		if (u <= l && r <= v) {
			return st[id]; 
		} 
		int mid = (l + r) / 2; 
		if (u > mid) return get(id * 2 + 1, mid + 1, r, u, v); 
		if (mid >= v) return get(id * 2, l, mid, u, v); 
		return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));  
	}
};

int a[N], n, d;
int last[N];
int mn[N]; 
int cur[N];
vector<int> compressed;

signed main(){
    ios_base::sync_with_stdio(false) ; 
    cin.tie(0) ; cout.tie(0) ; 
    if (fopen("INP.INP" ,"r")) {
        freopen("INP.INP" ,"r" , stdin) ;
        freopen("OUT.OUT" , "w" , stdout) ;
    }
    cin >> n >> d; 
    for (int i = 1; i <= n; i++) {
    	cin >> a[i]; 
    	compressed.pb(a[i]);
    }
    sort(compressed.begin(), compressed.end()); 
    compressed.erase(unique(compressed.begin(), compressed.end()), compressed.end()); 
    for (int i = 1; i <= n; i++) {
    	a[i] = upper_bound(compressed.begin(), compressed.end(), a[i]) - compressed.begin();
    }
    deque<int> dq;
    for (int i = 1; i <= n; i++) {
    	while(!dq.empty() && dq.front() <= i - d) dq.pop_front(); 
    	while(!dq.empty() && a[i] <= a[dq.back()]) dq.pop_back(); 
    	dq.push_back(i); 
    	mn[i] = a[dq.front()]; 
    }
    int sz = compressed.size(); 
    SegTree seg(sz); 
    for (int i = 1; i <= n; i++) {
    	last[i] = seg.get(1, 1, sz, a[i] + 1, sz) + 1;
    	seg.update(1, 1, sz, mn[i], i); 
    }
    seg = SegTree(sz); 
    int res = 0;
    priority_queue<pii> pq; 
    for (int i = 1; i <= sz; i++) cur[i] = n + 1;
    for (int i = n; i >= 1; i--) {
    	while(!pq.empty()) {
    		if (pq.top().F <= i + d) break; 
    		if (cur[pq.top().S] > i) seg.update(1, 1, sz, pq.top().S, 0); 
    		pq.pop();
    	}
    	int f = seg.get(1, 1, sz, a[i] + 1, sz); 
    	res = max(res, f + 1); 
    	cur[a[i]] = min(cur[a[i]], last[i]);
    	pq.push({cur[a[i]], a[i]}); 
    	seg.update(1, 1, sz, a[i], f + 1); 
    }
    cout << res;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen("INP.INP" ,"r" , stdin) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen("OUT.OUT" , "w" , stdout) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...