이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 300300;
int dp[N], arr[N], L[N];
struct SlideWin{
stack < int > L, R, S;
void add(int x) {
R.push(max(R.empty() ? 0 : R.top(), x));
S.push(x);
}
void del() {
if(L.empty()) {
for(; !S.empty();) {
L.push(max(L.empty() ? 0 : L.top(), S.top()));
S.pop();
R.pop();
}
}
L.pop();
}
int get_max() {
return max(L.empty() ? 0 : L.top(),
R.empty() ? 0 : R.top());
}
};
map < int, SlideWin > mp;
int lg2(int x) {
return 31 - __builtin_clz(x);
}
struct node{
int l, r, mx = 0;
node *left = NULL, *right = NULL;
node(int _l, int _r):l(_l), r(_r) {};
};
void check(node *x) {
if(x->left == NULL) {
x->left = new node(x->l, (x->l + x->r) / 2);
}
if(x->right == NULL) {
x->right = new node((x->l + x->r) / 2, x->r);
}
}
void update(int pos, node *x) {
if(x->l + 1 == x->r) {
x->mx = mp[x->l].get_max();
return;
}
check(x);
pos < x->left->r ?
update(pos, x->left) :
update(pos, x->right) ;
x->mx = max(x->left->mx, x->right->mx);
}
int get(int r, node *x) {
if(x->r <= r || x->mx == 0) {
return x->mx;
}
check(x);
int res = get(r, x->left);
if(x->right->l < r) {
res = max(res, get(r, x->right));
}
return res;
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, d;
cin >> n >> d;
if(n < 21) {
int n, d;
cin >> n >> d;
for(int i = 0; i < n; ++ i) {
cin >> arr[i];
}
int ans = 0;
for(int msk = (1<<n); msk --> 0;) {
int k = 0, tmp = msk, pr = -1, cr, mx = -1;
for(; tmp; tmp -= (-tmp&tmp)) {
cr = lg2(-tmp&tmp);
if(pr != -1 && cr - pr > d) {
k = 0;
break;
}
if(mx < arr[cr]) {
++ k;
mx = arr[cr];
}
pr = cr;
}
if(pr == n - 1) {
ans = max(ans, k);
}
}
cout << ans;
return 0;
}
stack < int > st;
node *root = new node(0, (1<<30));
for(int i = 1; i <= n; ++ i) {
cin >> arr[i];
L[i] = arr[L[i - 1]] < arr[i] ? i : L[i - 1];
if(i > d + 1) {
mp[arr[i - d - 1]].del();
update(arr[i - d - 1], root);
}
dp[i] = get(arr[i], root) + 1;
for(; !st.empty() && arr[st.top()] <= arr[i]; st.pop()) {
dp[i] = max(dp[i], dp[st.top()] + (arr[st.top()] < arr[i]));
}
for(int j = i - 1; j --> (i - d);) {
for(int cr = j; cr != L[cr] && arr[cr] <= arr[i]; cr = L[cr]) {
dp[i] = max(dp[i], dp[cr] + (arr[cr] < arr[i]));
}
}
st.push(i);
mp[arr[i]].add(dp[i]);
update(arr[i], root);
}
int mx = 0;
for(; !st.empty(); st.pop()) {
mx = max(mx, dp[st.top()]);
}
cout << dp[13] << ' ';
cout << mx;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
88 | main() {
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:96:12: warning: unused variable 'm' [-Wunused-variable]
96 | int n, m, d;
| ^
# | 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... |