이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define int long long
#define inf (int)1e18
#define en cout << "\n";
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
static struct FastInput
{
static constexpr int BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t chars_read = 0;
size_t buf_pos = 0;
FILE *in = stdin;
char cur = 0;
inline char get_char()
{
if (buf_pos >= chars_read)
{
chars_read = fread(buf, 1, BUF_SIZE, in);
buf_pos = 0;
buf[0] = (chars_read == 0 ? -1 : buf[0]);
}
return cur = buf[buf_pos++];
}
inline void tie(int) {}
inline explicit operator bool()
{
return cur != -1;
}
inline static bool is_blank(char c)
{
return c <= ' ';
}
inline bool skip_blanks()
{
while (is_blank(cur) && cur != -1)
{
get_char();
}
return cur != -1;
}
inline FastInput &operator>>(char &c)
{
skip_blanks();
c = cur;
return *this;
}
inline FastInput &operator>>(string &s)
{
if (skip_blanks())
{
s.clear();
do
{
s += cur;
} while (!is_blank(get_char()));
}
return *this;
}
template <typename T>
inline FastInput &read_integer(T &n)
{
// unsafe, doesn't check that characters are actually digits
n = 0;
if (skip_blanks())
{
int sign = +1;
if (cur == '-')
{
sign = -1;
get_char();
}
do
{
n += n + (n << 3) + cur - '0';
} while (!is_blank(get_char()));
n *= sign;
}
return *this;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, FastInput &>::type operator>>(T &n)
{
return read_integer(n);
}
#if !defined(_WIN32) || defined(_WIN64)
inline FastInput &operator>>(__int128 &n)
{
return read_integer(n);
}
#endif
template <typename T>
inline typename enable_if<is_floating_point<T>::value, FastInput &>::type operator>>(T &n)
{
// not sure if really fast, for compatibility only
n = 0;
if (skip_blanks())
{
string s;
(*this) >> s;
sscanf(s.c_str(), "%lf", &n);
}
return *this;
}
} fast_input;
#define cin fast_input
static struct FastOutput
{
static constexpr int BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t buf_pos = 0;
static constexpr int TMP_SIZE = 1 << 20;
char tmp[TMP_SIZE];
FILE *out = stdout;
inline void put_char(char c)
{
buf[buf_pos++] = c;
if (buf_pos == BUF_SIZE)
{
fwrite(buf, 1, buf_pos, out);
buf_pos = 0;
}
}
~FastOutput()
{
fwrite(buf, 1, buf_pos, out);
}
inline FastOutput &operator<<(char c)
{
put_char(c);
return *this;
}
inline FastOutput &operator<<(const char *s)
{
while (*s)
{
put_char(*s++);
}
return *this;
}
inline FastOutput &operator<<(const string &s)
{
for (int i = 0; i < (int)s.size(); i++)
{
put_char(s[i]);
}
return *this;
}
template <typename T>
inline char *integer_to_string(T n)
{
// beware of TMP_SIZE
char *p = tmp + TMP_SIZE - 1;
if (n == 0)
{
*--p = '0';
}
else
{
bool is_negative = false;
if (n < 0)
{
is_negative = true;
n = -n;
}
while (n > 0)
{
*--p = (char)('0' + n % 10);
n /= 10;
}
if (is_negative)
{
*--p = '-';
}
}
return p;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, char *>::type stringify(T n)
{
return integer_to_string(n);
}
#if !defined(_WIN32) || defined(_WIN64)
inline char *stringify(__int128 n)
{
return integer_to_string(n);
}
#endif
template <typename T>
inline typename enable_if<is_floating_point<T>::value, char *>::type stringify(T n)
{
sprintf(tmp, "%.17f", n);
return tmp;
}
template <typename T>
inline FastOutput &operator<<(const T &n)
{
auto p = stringify(n);
for (; *p != 0; p++)
{
put_char(*p);
}
return *this;
}
} fast_output;
#define cout fast_output
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if (x < 0)
x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if (x < 0)
x += mod;
}
struct SegmentTree
{
vector<int> st;
int n;
/// real n
SegmentTree(int _n = 0) : n(_n)
{
st.resize(4 * n + 10);
}
void resize(int _n)
{
n = _n;
st.resize(4 * n + 10);
}
void assign(int id, int l, int r, int x, int val)
{
if (x > r or x < l)
return;
if (l == r)
{
st[id] = val;
return;
}
int mid = (l + r) >> 1;
assign(id << 1, l, mid, x, val);
assign(id << 1 | 1, mid + 1, r, x, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return -inf;
if (u <= l && r <= v)
return st[id];
int mid = (l + r) >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void assign(int x, int val)
{
assign(1, 1, n, x, val);
}
};
struct SegmentTree1
{
vector<int> st;
int n;
/// real n
SegmentTree1(int _n = 0) : n(_n)
{
st.resize(4 * n + 10,inf);
}
void resize(int _n)
{
n = _n;
st.resize(4 * n + 10,inf);
}
void assign(int id, int l, int r, int x, int val)
{
if (x > r or x < l)
return;
if (l == r)
{
st[id] = val;
return;
}
int mid = (l + r) >> 1;
assign(id << 1, l, mid, x, val);
assign(id << 1 | 1, mid + 1, r, x, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return inf;
if (u <= l && r <= v)
return st[id];
int mid = (l + r) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void assign(int x, int val)
{
assign(1, 1, n, x, val);
}
};
int maxx[maxn][20];
int getmax(int l, int r)
{
int lg = __lg(r - l + 1);
return max(maxx[l][lg], maxx[r - (1 << lg) + 1][lg]);
}
int n, d, a[maxn], R[maxn];
pii arr[maxn];
int ff[maxn];
int f[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> d;
SegmentTree st(n);
SegmentTree1 t(n);
fo(i, 1, n)
{
cin >> a[i];
R[i] = n + 1;
t.assign(i, a[i]);
}
fo(i, d, n)
{
maxx[i][0] = t.get(i - d,i - 1);
}
fo(j, 1, 19) fo(i, 1, n)
maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << j - 1)][j - 1]);
fo(i, 1, n)
{
int l = i + d + 1, r = n, ans = n + 1;
while (l <= r)
{
int mid = l + r >> 1;
if (getmax(i + d + 1, mid) > a[i])
ans = mid, r = mid - 1;
else
l = mid + 1;
}
R[i] = ans;
}
fo(i, 1, n) arr[i] = {a[i], i};
sort(arr + 1, arr + n + 1, [](pii a, pii b)
{ return a.fi > b.fi or (a.fi == b.fi and a.se < b.se); });
fo(i, 1, n)
{
auto [val, id] = arr[i];
f[id] = max(1ll, st.get(id + 1,R[id] - 1) + 1);
st.assign(id, f[id]);
}
cout << *max_element(f + 1, f + n + 1);
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:385:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
385 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:394:13: warning: passing NULL to non-pointer argument 1 of 'void FastInput::tie(long long int)' [-Wconversion-null]
394 | cin.tie(NULL);
| ^~~~
Main.cpp:9:13: note: declared here
9 | #define int long long
| ^~~~~~~~~
Main.cpp:63:21: note: in expansion of macro 'int'
63 | inline void tie(int) {}
| ^~~
Main.cpp:409:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
409 | maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << j - 1)][j - 1]);
| ~~^~~
Main.cpp:415:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
415 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:390:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
390 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:391:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
391 | freopen(name ".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... |