This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define task "JOI21_FINANCIAL"
using namespace std;
const int N = 3e5 + 5;
int n, A[N], D;
set < int > Set;
int CP[N], cnt;
void compress()
{
for(int i = 1; i <= n; i++) Set.insert(A[i]);
for(int j : Set) CP[++cnt] = j;
for(int i = 1; i <= n; i++)
A[i] = lower_bound(CP + 1, CP + cnt + 1, A[i]) - CP;
}
namespace sub2
{
int dp[405][405], ans;
void solve()
{
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++) dp[i][j] = -1e9;
dp[0][0] = 0;
for(int i = 0; i < n; i++)
{
if(i)
dp[i][A[i]] = max(dp[i][A[i]], 1);
for(int j = 0; j <= n; j++)
for(int t = i + 1; t <= i + D; t++)
if(t <= n)
dp[t][max(A[t], j)] = max(dp[t][max(A[t], j)], dp[i][j] + (A[t] > j));
}
for(int j = 0; j <= n; j++) ans = max(ans, dp[n][j]);
cout << ans;
}
}
namespace sub3
{
int dp[7005], ans;
void solve()
{
for(int i = 1; i <= n; i++)
{
dp[i] = 1;
bool ok = true;
int cnt = 0;
for(int j = i - 1; j >= 1; j--)
{
if(ok && A[j] < A[i]) dp[i] = max(dp[i], dp[j] + 1);
if(A[j] >= A[i]) cnt++;
else cnt = 0;
if(cnt >= D) ok = false;
}
ans = max(ans, dp[i]);
}
cout << ans;
}
}
namespace sub4
{
int L[N], St[N], top, ans;
priority_queue < int > pq;
void solve()
{
for(int i = 1; i <= n; i++)
{
while(top && A[i] > A[St[top]]) top--;
L[i] = St[top] + 1;
St[++top] = i;
}
for(int i = n; i >= 1; i--)
{
pq.push(L[i]);
while(pq.top() > i) pq.pop();
ans = max(ans, (int)pq.size());
}
cout << ans;
}
}
namespace sub5
{
int ans;
struct FenwickTree
{
int BIT[N];
int get(int p)
{
int res = 0;
for( ; p ; p -= p & -p)
res = max(res, BIT[p]);
return res;
}
void update(int p, int val)
{
for( ; p <= 3e5; p += p & -p)
BIT[p] = max(BIT[p], val);
}
} FT;
void solve()
{
for(int i = 1; i <= n; i++)
{
int tmp = FT.get(A[i] - 1) + 1;
FT.update(A[i], tmp);
ans = max(ans, tmp);
}
cout << ans;
}
}
namespace sub6
{
int Min[N], R[N];
void BuildMin()
{
stack < int > st;
st.push(n+1);
for(int i = n; i >= 1; i--)
{
while(st.size()>1 && A[st.top()]>A[i]) st.pop();
R[i] = st.top() - 1;
st.push(i);
}
int p = 1;
for(int i = 1; i <= n - D + 1; i++)
{
int j = i + D - 1;
while(!(i <= p && p <= j && R[p] >= j) && p<=n) p++;
Min[i] = A[p];
}
///Min[i] la GTNN trong doan [i; i + D - 1]
}
struct FenwickTree
{
int BIT[N];
int get(int p)
{
int res = 0;
for( ; p <= 3e5; p += p & -p)
res = max(res, BIT[p]);
return res;
}
void update(int p, int val)
{
for( ; p ; p -= p & -p)
BIT[p] = max(BIT[p], val);
}
} FT;
struct SegmentTree
{
int ST[4*N];
int get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return 0;
if(u <= l && r <= v) return ST[id];
int mid = (l + r)/2;
return max(get(id*2, l, mid, u, v), get(id*2+1, mid + 1, r, u, v));
}
void update(int id, int l, int r, int x, int val)
{
if(l > x || r < x) return;
if(l == r)
{
ST[id] = max(ST[id], val);
return;
}
int mid = (l + r)/2;
update(id*2, l, mid, x, val);
update(id*2 + 1, mid + 1, r, x, val);
ST[id] = max(ST[id*2], ST[id*2 + 1]);
}
} IT;
int leftmost[N], ans = 0, ord[N];
void solve()
{
BuildMin();
for(int i = 1; i <= n; i++)
{
leftmost[i] = FT.get(A[i]) + 1;
///True in [pos, i - 1]
if(i >= D) FT.update(Min[i - D + 1], i - D + 1);
}
for(int i = 1; i <= n; i++) ord[i] = i;
sort(ord + 1, ord + n + 1, [&] (int x, int y){
if(A[x] == A[y]) return x > y; else return A[x] < A[y];
});
for(int j = 1; j <= n; j++)
{
int i = ord[j];
int tmp = IT.get(1, 1, n, leftmost[i], i - 1) + 1;
ans = max(ans, tmp);
IT.update(1, 1, n, i, tmp);
}
cout << ans;
}
}
int main()
{
if(fopen(task ".inp","r"))
{
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> D;
for(int i = 1; i <= n; i++) cin >> A[i];
compress();
if(n <= 400) sub2::solve();
else if(n <= 7000) sub3::solve();
else if(D == 1) sub4::solve();
else if(D == n) sub5::solve();
else sub6::solve();
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
200 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |