이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb emplace_back
#define x first
#define y second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pt;
typedef pair<ll, ll> ptll;
//typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//typedef gp_hash_table<int, int> table;
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,sse3,ssse3,sse4.1,sse4.2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("inline")
*/
//-------------------------------------------
const int MAXN = 3e5 + 10;
int a[MAXN];
void coordinate_compression(int n, int t){
vector<pt> coords(n);
for (int i = 0; i < n; i++)
coords[i] = {a[i], i};
sort(all(coords));
a[coords[0].y] = 1;
for (int i = 1; i < n; i++){
if (coords[i].x != coords[i - 1].x)
t++;
a[coords[i].y] = t;
}
}
int jmp[MAXN];
int p[MAXN];
int r[MAXN];
int val[MAXN];
vector<int> arev[MAXN];
int get(int x){
if (x == p[x])
return x;
return p[x] = get(p[x]);
}
void Union(int x, int y){
x = get(x), y = get(y);
if (r[x] > r[y])
swap(x, y);
p[x] = y;
if (r[x] == r[y])
r[y]++;
val[y] = max(val[y], val[x]);
}
void longest_jump(int n, int d){
iota(p, p + n, 0);
iota(val, val + n, 0);
fill(r, r + n, 1);
for (int i = 0; i < n; i++){
arev[a[i]].push_back(i);
}
set<int> used;
for (int x = 1; x <= n + 1; x++){
for (int i: arev[x]){
used.insert(i);
auto it = used.find(i);
if (it != used.begin()){
int j = *(--it);
if (i - j <= d)
Union(i, j);
it++;
}
it++;
if (it != used.end()){
int j = *it;
if (j - i <= d)
Union(i, j);
}
}
for (int i: arev[x]){
jmp[i] = min(n - 1, val[get(i)] + d) + 1;
}
}
}
int tree[MAXN << 2];
void upd(int v, int lt, int rt, int pos, int nw){
if (rt - lt == 1){
tree[v] = nw;
return;
}
int m = (lt + rt) >> 1;
if (pos < m)
upd(v << 1, lt, m, pos, nw);
else
upd(v << 1 | 1, m, rt, pos, nw);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
int getmax(int v, int lt, int rt, int ql, int qr){
if (rt <= ql || qr <= lt)
return 0;
if (ql <= lt && rt <= qr)
return tree[v];
int m = (lt + rt) >> 1;
return max(getmax(v << 1, lt, m, ql, qr), getmax(v << 1 | 1, m, rt, ql, qr));
}
int dp[MAXN];
int inds[MAXN];
int right_border[MAXN];
void calc_dp(int n){
fill(dp, dp + n, 1);
set<pt> st;
for (int i = 0; i < n; i++){
while (!st.empty() && (*st.begin()).x == i){
int prv = (*st.begin()).y;
upd(1, 1, MAXN + 1, prv, 0);
st.erase(st.begin());
}
int mx = getmax(1, 1, MAXN + 1, 1, right_border[a[i]]) + 1;
dp[i] = mx;
upd(1, 1, MAXN + 1, inds[i], mx);
st.insert({jmp[i], inds[i]});
}
}
void calc_right_border(int n){
vector<pt> srt(n);
for (int i = 0; i < n; i++)
srt[i] = {a[i], i};
sort(all(srt));
fill(right_border, right_border + MAXN + 1, MAXN + 2);
for (int i = 0; i < n; i++){
inds[srt[i].y] = i + 1;
right_border[srt[i].x] = min(right_border[srt[i].x], i + 1);
}
}
void run(){
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++)
cin >> a[i];
coordinate_compression(n, 1);
longest_jump(n, d);
calc_right_border(n);
calc_dp(n);
int ans = 1;
for (int i = 0; i < n; i++){
if (jmp[i] == n)
ans = max(ans, dp[i]);
}
cout << ans;
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#else
fastio();
//freopen("", "r", stdin);
//freopen("", "w", stdout);
#endif
auto start = chrono::high_resolution_clock::now();
run();
auto stop = chrono::high_resolution_clock::now();
#ifdef LOCAL
cerr << "Program finished in " << (ld)chrono::duration_cast<chrono::milliseconds>(stop - start).count() / 1e3 << " sec";
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:186:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
186 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
Main.cpp:188:10: warning: variable 'stop' set but not used [-Wunused-but-set-variable]
188 | auto stop = chrono::high_resolution_clock::now();
| ^~~~
Main.cpp: In function 'void calc_right_border(int)':
Main.cpp:154:9: warning: array subscript 300011 is outside array bounds of 'int [300010]' [-Warray-bounds]
154 | fill(right_border, right_border + MAXN + 1, MAXN + 2);
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:131:5: note: while referencing 'right_border'
131 | int right_border[MAXN];
| ^~~~~~~~~~~~
# | 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... |