이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author : daohuyenchi
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define db double
#define i32 int32_t
#define i64 int64_t
#define ll long long
//
#define fi first
#define se second
// #define int long long // consider carefully
//
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define PAIR make_pair
// TIME IS LIMITED ...
#define rep(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define repd(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define repv(v, H) for(auto &v: H)
// REFLECT ON THE PAST ...
#define RESET(c, x) memset(c, x, sizeof(c))
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1LL)
#define ONBIT(mask, i) ((mask) | (1LL << (i)))
#define OFFBIT(mask, i) ((mask) &~ (1LL << (i)))
#define COUNTBIT __builtin_popcountll
// 30 / 1 / 2024 ? love is zero... start from zero
#define PB push_back
#define EB emplace_back
#define vi vector<int>
#define vll vector<ll>
#define lwb lower_bound
#define upb upper_bound
#define all(v) (v).begin(), (v).end()
#define special(H) (H).resize(distance(H.begin(), unique(all(H))))
//
#define sp ' '
#define nl '\n'
#define EL {cerr << '\n';}
#define yes "YES"
#define no "NO"
#define Log2(n) (63 - __builtin_clzll(n))
#define left __left__
#define right __right__
//____________________________________________________________________
template <class X, class Y> bool maximize(X &a, const Y &b) {
if(a < b) return a = b, true;
return false;
}
template <class X, class Y> bool minimize(X &a, const Y &b) {
if(a > b) return a = b, true;
return false;
}
template <class... T>
void print(T&&... n) {
using exp = int[];
exp{0, (cerr << n << sp, 0)...};
cerr << nl;
}
template <class... T>
void print2(T&&... n) {
using exp = int[];
exp{0, (cerr << n << sp, 0)...};
// cerr << sp;
}
template <class T, class... C>
void assign(int n, T v, C&&... a) {
using e = int[];
e{(a.assign(n, v), 0)...};
}
template <class... C>
void resize(int n, C&&... a) {
using e = int[];
e{(a.resize(n), 0)...};
}
template <class T>
using vector2d = vector<vector<T>>;
template <class T>
using vector3d = vector<vector2d<T>>;
template <class T> int ssize(T &a) { return (int) a.size(); }
//____________________________________________________________________
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
const int MOD = 1000000007;
// const int MOD[2] = {1000000009, 998244353};
template<class X> void modmize(X &x, int cur_Mod = MOD) {
if(x >= cur_Mod) x -= cur_Mod;
if(x < 0) x += cur_Mod;
}
const long long oo = 1e18 + 7;
const int INF = 2e9;
const int nmax = 3e5 + 2;
const int MAX = 2e5;
const int base = 311;
const db eps = 1e-6;
const int block = 500;
static const double PI = acos(-1.0);
//____________________________________________________________________
int n, D;
int a[nmax];
pii b[nmax];
struct HuyenChi {
int st[nmax * 4];
void upd(int id, int l, int r, int i, int val) {
// if (l > i || r < i) return;
if (l == r) {
st[id] = val;
return;
}
int mid = (l + r) / 2;
if (i <= mid) upd(id * 2, l, mid, i, val);
else upd(id * 2 + 1, mid + 1, r, i, val);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
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));
}
} chi;
struct Node {
int mx, lf;
Node() {
// sum = 0;
mx = 0;
lf = nmax * 2;
}
Node(int x2, int x3) {
// sum = x1;
mx = x2;
lf = x3;
}
Node operator + (const Node &ot) {
return Node(max(mx, ot.mx), min(lf, ot.lf));
}
} ;
struct HaMy {
// pii st[nmax * 4];
Node st[nmax * 4];
void upd(int id, int l, int r, int i, Node val) {
// if (l > i || r < i) return;
if (l == r) {
st[id] = val;
return;
}
int mid = (l + r) / 2;
if (i <= mid) upd(id * 2, l, mid, i, val);
else upd(id * 2 + 1, mid + 1, r, i, val);
st[id] = st[id * 2] + st[id * 2 + 1];
}
int wot(int id, int l, int r) {
if (l == r) return st[id].lf == l ? l : -1;
int mid = (l + r) / 2;
if (st[id * 2 + 1].lf <= n) return wot(id * 2 + 1, mid + 1, r);
return wot(id * 2, l, mid);
}
int cal(int id, int l, int r, int i) {
if (st[id].lf > n) return -1;
if (r <= i) return wot(id, l, r);
int mid = (l + r) / 2;
if (i <= mid) {
return cal(id * 2, l, mid, i);
}
else {
// mid < i
if (st[id * 2 + 1].lf <= i) return cal(id * 2 + 1, mid + 1, r, i);
else return cal(id * 2, l, mid, i);
}
}
// Node 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 (v <= mid) return get(id * 2, l, mid, u, v);
// else if (u > mid) return get(id * 2 + 1, mid + 1, r, u, v);
// else {
// return get(id * 2 + 1, mid + 1, r, u, v) + get(id * 2 + 1, mid + 1, r, u, v);
// }
// }
int get_mx(int id, int l, int r, int u, int v) {
if (u <= l && r <= v) return st[id].mx;
int mid = (l + r) / 2;
if (v <= mid) return get_mx(id * 2, l, mid, u, v);
else if (u > mid) return get_mx(id * 2 + 1, mid + 1, r, u, v);
else {
return max(get_mx(id * 2 + 1, mid + 1, r, u, v), get_mx(id * 2 + 1, mid + 1, r, u, v));
}
}
int walk_lf(int id, int l, int r, int i) {
if (l == r) return st[id].mx <= D ? l : -1;
// if (r <= i) {
// if (st[id].mx <= D) return l;
// }
int mid = (l + r) / 2;
if (i <= mid) {
return walk_lf(id * 2, l, mid, i);
}
else {
if (get_mx(id * 2 + 1, mid + 1, r, mid + 1, i) <= D) return max(mid + 1, walk_lf(id * 2, l, mid, i));
else return walk_lf(id * 2 + 1, mid + 1, r, i);
}
}
int get_lf(int id, int l, int r, int u, int v) {
if (u <= l && r <= v) return st[id].lf;
int mid = (l + r) / 2;
if (v <= mid) return get_lf(id * 2, l, mid, u, v);
else if (u > mid) return get_lf(id * 2 + 1, mid + 1, r, u, v);
else {
return min(get_lf(id * 2 + 1, mid + 1, r, u, v), get_lf(id * 2 + 1, mid + 1, r, u, v));
}
}
} lery;
int dp[nmax];
vector <int> List[nmax];
void tintingyn() {
cin >> n >> D;
rep (i, 1, n) {
cin >> a[i];
// b[i] = {a[i], i};
}
vector <int> zi;
int Lim;
rep (i, 1, n) zi.push_back(a[i]);
sort(all(zi));
special(zi);
rep (i, 1, n) {
a[i] = lower_bound(all(zi), a[i]) - zi.begin() + 1;
}
Lim = ssize(zi);
// vector2d <int> List;
// assign(Lim + 1, vector <int>(), List);
rep (i, 1, n) {
List[a[i]].push_back(i);
}
// vector <int> dp(n + 2, 0);
rep (v, 1, Lim) {
// EL
// print("{");
// print2(" ");
// rep (i, 1, n) {
// cerr << lery.get(1, 1, n, i, i).sum << sp;
// // print(lery.get(1, 1, n, i, i).sum, lery.get(1, 1, n, i, i).lf);
// }
// EL
// print("}");EL
repv (id, List[v]) {
dp[id] = 1;
int mx_j = lery.cal(1, 1, n, id);
// print(mx_j, id);
if (mx_j > 1 && id - mx_j <= D) {
// int l = 1, r = mx_j;
// int res = -1;
// while (l <= r) {
// int mid = (l + r) / 2;
// if (lery.get_mx(1, 1, n, mid, mx_j - 1) <= D) {
// res = mid;
// r = mid - 1;
// }
// else l = mid + 1;
// }
int res = lery.walk_lf(1, 1, n, mx_j - 1);
// print(res, id);
if (res != -1) {
dp[id] = chi.get(1, 1, n, res, mx_j) + 1;
}
}
else if (mx_j == 1 && id - mx_j <= D) {
dp[id] = dp[mx_j] + 1;
}
}
repv (id, List[v]) {
chi.upd(1, 1, n, id, dp[id]);
int mx_j = lery.cal(1, 1, n, id);
if (mx_j != -1) {
lery.upd(1, 1, n, mx_j, Node(id - mx_j, mx_j));
}
int cur = lery.get_lf(1, 1, n, id, n);
// if (cur.lf <= n) {
lery.upd(1, 1, n, id, Node(cur <= n ? cur - id : n + 1, id));
// }
}
}
int ans = chi.get(1, 1, n, 1, n);
cout << ans << nl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//________________________________________________________________
#define TASK "1"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
//________________________________________________________________
// CODE FROM HERE ...!
int num_test = 1;
// cin >> num_test;
while(num_test--) {
tintingyn();
}
cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" << nl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:362:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
362 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:363:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
363 | 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... |