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 ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Financial Report"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 3e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, D;
vector<int> Del[maxn];
int a[maxn], vt[maxn], dp[maxn];
pair<int, int> P[maxn];
struct DSU {
int lab[maxn];
int get_rt(int u) { return lab[u] <= 0 ? u : lab[u] = get_rt(lab[u]); }
int siz(int u) { return -lab[get_rt(u)]; }
void uni(int u, int v) {
int p = get_rt(u), q = get_rt(v); if(p == q || !lab[p] || !lab[q]) return;
if(lab[p] > lab[q]) swap(p, q);
lab[p] += lab[q], lab[q] = p;
}
void Turn(int x) {
lab[x] = -1;
uni(x, x - 1), uni(x, x + 1);
}
} Dsu;
struct IT {
int st[4 * maxn], store[4 * maxn];
void build(int id = 1, int l = 0, int r = n) {
store[id] = st[id] = 0;
if(l == r) return;
int mid = (l + r) >> 1; build(_left), build(_right);
}
void down(int id) {
int val = store[id];
fi(i, 0, 1) st[id * 2 + i] += val, store[id * 2 + i] += val;
store[id] = 0;
}
void update(int u, int v, int val, int id = 1, int l = 0, int r = n)
{
if(l > v || r < u) return;
if(u <= l && r <= v) {
st[id] += val, store[id] += val;
return;
}
down(id);
int mid = (l + r) >> 1;
update(u, v, val, _left), update(u, v, val, _right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int u, int v, int id = 1, int l = 0, int r = n)
{
if(l > v || r < u) return 0;
if(u <= l && r <= v) return st[id];
down(id);
int mid = (l + r) >> 1;
return max(get(u, v, _left), get(u, v, _right));
}
int Find(int x, int val, int id = 1, int l = 0, int r = n)
{
if(r < x || st[id] < val) return -1;
if(l == r) return l;
down(id);
int mid = (l + r) >> 1;
int w = Find(x, val, _left);
if(w == -1) w = Find(x, val, _right);
return w;
}
} St;
void Init() {
St.build();
vector<int> pen;
auto update = [&](int x) {
int pre_len = Dsu.siz(x - 1), nex_len = Dsu.siz(x + 1);
St.update(x, x + nex_len, pre_len + 1);
Dsu.Turn(x);
};
fid(i, n, 1) {
if(pen.empty() || P[i].first != a[pen.back()]) {
for(int x : pen) update(x);
pen = {P[i].second};
} else pen.push_back(P[i].second);
int nex = St.Find(P[i].second, D);
if(nex != -1) Del[nex + 1].push_back(P[i].second);
}
}
void solve()
{
cin >> n >> D;
fi(i, 1, n) cin >> a[i], P[i] = {a[i], i};
sort(P + 1, P + 1 + n);
fi(i, 1, n) vt[P[i].second] = i;
Init(), St.build();
fi(i, 1, n) {
for(int x : Del[i]) St.update(vt[x], vt[x], -dp[x]);
pair<int, int> val = {a[i], -1e9};
int p = lower_bound(P + 1, P + 1 + n, val) - P - 1;
dp[i] = St.get(0, p) + 1;
St.update(vt[i], vt[i], dp[i]);
}
cout << *max_element(dp + 1, dp + 1 + n);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
153 | freopen(Neco".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... |