이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <queue>
#include <unordered_set>
#include <vector>
#ifndef EVAL
#include <format>
#include <iostream>
template <typename... Args>
void print(std::format_string<Args...> fmt, Args &&...args) {
std::cerr << std::format(fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void println(std::format_string<Args...> fmt, Args &&...args) {
std::cerr << std::format(fmt, std::forward<Args>(args)...) << '\n';
}
#else
#define print(...) ((void)0)
#define println(...) ((void)0)
#endif
int n;
std::vector<int> h;
std::map<int, int> data;
void init(int N, std::vector<int> H) {
n = N;
h = H;
int towers = 0;
struct D {
int prev;
int max_to_prev;
int next;
int max_to_next;
int pos;
};
std::vector<D> adj;
for (int i = 0; i < N; i++) {
towers += 1;
int max_to_prev = H[i];
int max_to_next = H[i];
if (i != 0) max_to_prev = std::max(max_to_prev, H[i - 1]);
if (i + 1 != N) max_to_next = std::max(max_to_next, H[i + 1]);
adj.push_back({.prev = i - 1,
.max_to_prev = max_to_prev,
.next = i + 1 == N ? -1 : i + 1,
.max_to_next = max_to_next,
.pos = i});
}
struct PQItem {
int priority;
int from;
int to;
bool operator<(const PQItem &rhs) const {
return priority > rhs.priority;
}
};
std::vector<bool> active(adj.size(), true);
std::priority_queue<PQItem> pq;
for (int i = 1; i < adj.size(); i++) {
pq.push({.priority = 0, .from = i - 1, .to = i});
}
while (!pq.empty()) {
auto [priority, from, to] = pq.top();
pq.pop();
if (!active[from] || !active[to]) continue;
data.insert_or_assign(priority, towers);
if (H[adj[from].pos] > H[adj[to].pos]) {
int max = adj[to].max_to_prev =
std::max(adj[from].max_to_prev, adj[to].max_to_prev);
adj[to].prev = adj[from].prev;
if (adj[to].prev != -1) {
adj[adj[to].prev].next = to;
pq.push({.priority = max - std::max(H[adj[adj[to].prev].pos],
H[adj[to].pos]),
.from = adj[to].prev,
.to = to});
}
towers -= 1;
active[from] = false;
} else {
int max = adj[from].max_to_next =
std::max(adj[from].max_to_next, adj[to].max_to_next);
adj[from].next = adj[to].next;
if (adj[from].next != -1) {
adj[adj[from].next].prev = from;
pq.push({.priority = max - std::max(H[adj[from].pos],
H[adj[adj[from].next].pos]),
.from = from,
.to = adj[from].next});
}
towers -= 1;
active[to] = false;
}
}
data.insert_or_assign(2'000'000'000, towers);
}
int max_towers(int L, int R, int D) {
if (L == R) return 1;
R += 1;
return data.lower_bound(D)->second;
}
컴파일 시 표준 에러 (stderr) 메시지
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<init(int, std::vector<int>)::D>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 1; i < adj.size(); i++) {
| ~~^~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |