이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 3e5 + 3;
const ll mod = 1e9 + 7;
int n, D;
int maxVal[MAX << 1];
void modify(int i, int val) {
i += n;
maxVal[i] = val;
for (; i > 1; i >>= 1) maxVal[i >> 1] = max(maxVal[i], maxVal[i ^ 1]);
}
int get(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, maxVal[l++]);
if (r & 1) res = max(res, maxVal[--r]);
}
return res;
}
void Solve() {
cin >> n >> D;
vector <int> a(n);
vector <int> id(n);
vector <int> pa(n);
vector <int> mn(n);
vector <int> dp(n);
for (int &x : a) cin >> x;
iota(pa.begin(), pa.end(), 0);
iota(mn.begin(), mn.end(), 0);
iota(id.rbegin(), id.rend(), 0);
sort(id.begin(), id.end(), [&](const int &i, const int &j) {
return a[i] < a[j];
});
function <int(int)> findpa = [&](int i) -> int {
return i == pa[i] ? i : pa[i] = findpa(pa[i]);
};
auto join = [&](int u, int v) -> void {
u = findpa(u);
v = findpa(v);
if (u == v) return;
pa[v] = u;
mn[u] = min(mn[u], mn[v]);
};
set <int> S;
for (int &i : id) {
auto p = S.lower_bound(i);
if (p != S.end() && *p - i <= D) join(*p, i);
if (p != S.begin() && i - *(prev(p)) <= D) join(i, *prev(p));
S.insert(i);
dp[i] = get(mn[findpa(i)], i) + 1;
modify(i, dp[i]);
}
cout << *max_element(dp.begin(), dp.end());
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:75:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:76:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | 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... |