제출 #1335825

#제출 시각아이디문제언어결과실행 시간메모리
1335825arkanefuryFinancial Report (JOI21_financial)C++20
0 / 100
15 ms2004 KiB
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define ins insert
#define lb lower_bound
#define F first
#define S second
#define int long long
#define pii pair<int, int>
#define sz size()
#define in insert
#define len(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define FOR(x, n, m, d) for(int x = n; x <= m; x += d)
#define FORR(x, n, m, d) for(int x = n; x >= m; x -= d)
#define nikita ios_base::sync_with_stdio(0), cin.tie(0);
const int N = 2e5 + 15;
int n, m, x, y, l, r, k, P = 7, mod = 1e9+7, ans, sum, cnt, a[N], timer  = 1, c[N];
int up[N][21], val[N][21];
void solve(){
	cin >> n >> m;
	FOR(i, 1, n, 1){
		cin >> a[i];
	}
	stack<int>st, st1;
	FORR(i, n, 1, 1){
		while(!st.empty() && a[st.top()] < a[i])up[st.top()][0] = i, st.pop();
		st.push(i);
	}
	FORR(i, n, 1, 1){
		while(!st1.empty() && a[st1.top()] > a[i])c[st1.top()] = i, st1.pop();
		st1.push(i);
	}
	FOR(i, 1, log2(m), 1){
		FOR(j, 1, n, 1)up[j][i] = up[up[j][i-1]][i-1];
	}
	FOR(i, 1, n, 1){
		k = c[i];
			FORR(j, log2(m), 0, 1){
				if(a[up[k][j]] < a[i] && i-up[k][j] <= m)val[i][0] = max(val[i][0], val[k][j]), k = up[k][j];
			}
			if(a[k] < a[i] && i-k <= m)val[i][0] = max(val[i][0], val[k][0]);
		val[i][0]++;
		if(a[i-1] == a[i])val[i][0] = max(val[i][0], val[i-1][0]);
		FOR(j, 1, log2(m), 1)val[i][j] = max(val[i][j-1], val[up[i][j-1]][j-1]);
		ans = max(ans, val[i][0]);
	}
	cout << ans;
}
signed main(){
	nikita
	int t=1;
	// cin >> t;
	while(t--)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...