some programs!!

This commit is contained in:
rogerwang2008 committed 2026-09-28 14:05:06 +08:00
1 parent ed054247d5
commit 71f1022dea
38 files changed
+1037 -281

No files matched your search

@@ -0,0 +1,111 @@
#include <algorithm>
#include <iostream>
#include <queue>
constexpr size_t MAX_N = 1e6 + 1;
constexpr unsigned long long SOLVE_1_THRESHOLD = 0;
template <typename T>
class QueueWithMinMax {
protected:
std::deque<T> max_queue, min_queue;
// std::priority_queue<T, std::vector<T>, std::greater<>> min_queue;
public:
std::queue<T> q;
void push(const T& x) {
q.push(x);
while (!max_queue.empty() && max_queue.back() < x) {
max_queue.pop_back();
}
max_queue.push_back(x);
while (!min_queue.empty() && min_queue.back() > x) {
min_queue.pop_back();
}
min_queue.push_back(x);
}
void pop() {
if (!max_queue.empty() && q.front() >= max_queue.front()) {
max_queue.pop_front();
}
if (!min_queue.empty() && q.front() <= min_queue.front()) {
min_queue.pop_front();
}
q.pop();
}
[[nodiscard]] T front() const { return q.front(); }
[[nodiscard]] T get_max() const {
return max_queue.front();
}
[[nodiscard]] T get_min() const {
return min_queue.front();
}
};
int n, window_length;
int nums[MAX_N], max_nums[MAX_N], min_nums[MAX_N];
QueueWithMinMax<int> queue;
void solve_1() {
for (int i = 0; i + window_length <= n; i++) {
max_nums[i] = *std::max_element(nums + i, nums + i + window_length);
min_nums[i] = *std::min_element(nums + i, nums + i + window_length);
}
}
void solve_2() {
for (int i = 0; i < window_length; i++) {
queue.push(nums[i]);
}
for (int i = 0; i + window_length < n; i++) {
max_nums[i] = queue.get_max();
min_nums[i] = queue.get_min();
queue.pop();
queue.push(nums[i + window_length]);
}
max_nums[n - window_length] = queue.get_max();
min_nums[n - window_length] = queue.get_min();
}
void print_ans() {
for (int i = 0; i <= n - window_length; i++) {
std::cout << min_nums[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i <= n - window_length; i++) {
std::cout << max_nums[i] << " ";
}
std::cout << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> window_length;
if (window_length > n) { window_length = n; }
for (int i = 0; i < n; i++) {
std::cin >> nums[i];
}
if (1ll * n * window_length < SOLVE_1_THRESHOLD) {
solve_1();
} else {
solve_2();
}
// solve_1();
// print_ans();
// solve_2();
print_ans();
return 0;
}
@@ -0,0 +1,38 @@
#include <algorithm>
#include <iostream>
#include <queue>
constexpr size_t MAX_N = 1e6 + 1;
int n, window_length;
int nums[MAX_N], max_nums[MAX_N], min_nums[MAX_N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> window_length;
if (window_length > n) { window_length = n; }
for (int i = 0; i < n; i++) {
std::cin >> nums[i];
}
for (int i = 0; i + window_length <= n; i++) {
max_nums[i] = *std::max_element(nums + i, nums + i + window_length);
min_nums[i] = *std::min_element(nums + i, nums + i + window_length);
}
for (int i = 0; i <= n - window_length; i++) {
std::cout << min_nums[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i <= n - window_length; i++) {
std::cout << max_nums[i] << " ";
}
std::cout << std::endl;
return 0;
}
+165
View File
@@ -0,0 +1,165 @@
#include <iostream>
#include <stack>
#include <vector>
#include <variant>
#include <cassert>
class Token {
protected:
std::variant<char, int> token_variant_;
public:
explicit Token(char c) : token_variant_(c) {}
explicit Token(int x) : token_variant_(x) {}
[[nodiscard]] bool is_operator() const {
return std::holds_alternative<char>(token_variant_);
}
[[nodiscard]] char get_operator() const {
if (!is_operator()) { throw std::invalid_argument("Not an operator"); }
return std::get<char>(token_variant_);
}
[[nodiscard]] int get_number() const {
if (is_operator()) { throw std::invalid_argument("Is an operator"); }
return std::get<int>(token_variant_);
}
[[nodiscard]] int get_priority() const {
if (get_operator() == '+' || get_operator() == '-') { return 1; }
if (get_operator() == '*' || get_operator() == '/') { return 2; }
throw std::invalid_argument("Unknown priority");
}
template <typename T1, typename T2>
[[nodiscard]] auto calc_operator(T1 a, T2 b) const {
switch (get_operator()) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default: throw std::invalid_argument("Uncalculatable operator");
}
}
bool operator==(char c) const {
if (!is_operator()) { return false; }
return get_operator() == c;
}
bool operator==(int x) const {
if (is_operator()) { return false; }
return get_number() == x;
}
template <typename T>
bool operator!=(T x) {
return !(*this == x);
}
friend std::ostream& operator<<(std::ostream& os, Token token) {
if (token.is_operator()) {
os << token.get_operator();
} else {
os << '[' << token.get_number() << ']';
}
return os;
}
};
using Expression = std::vector<Token>;
std::ostream& operator<<(std::ostream& os, const Expression& expression) {
for (const auto& token : expression) {
os << token;
}
return os;
}
Expression parse_string(const std::string& str) {
Expression expression;
std::string temp_token;
for (char c : str) {
if (!(c >= '0' && c <= '9')) {
if (!temp_token.empty()) {
expression.emplace_back(std::stoi(temp_token));
temp_token.clear();
}
expression.emplace_back(c);
} else {
temp_token += c;
}
}
if (!temp_token.empty()) { expression.emplace_back(std::stoi(temp_token)); }
return expression;
}
Expression convert_to_postfix(const Expression& infix_expression) {
Expression postfix_expression;
std::stack<Token> operators;
for (const auto& token : infix_expression) {
if (!token.is_operator()) {
postfix_expression.push_back(token);
continue;
}
if (token.get_operator() == '(') {
operators.push(token);
continue;
}
if (token.get_operator() == ')') {
while (operators.top().get_operator() != '(') {
postfix_expression.push_back(operators.top());
operators.pop();
}
operators.pop();
continue;
}
while (!operators.empty() && operators.top() != '('
&& token.get_priority() <= operators.top().get_priority()) {
postfix_expression.push_back(operators.top());
operators.pop();
}
operators.push(token);
}
while (!operators.empty()) {
postfix_expression.push_back(operators.top());
operators.pop();
}
return postfix_expression;
}
int calculate_postfix(const Expression& postfix_expression) {
std::stack<int> numbers;
for (const auto& token : postfix_expression) {
if (!token.is_operator()) {
numbers.push(token.get_number());
continue;
}
int b = numbers.top();
numbers.pop();
int a = numbers.top();
numbers.pop();
numbers.push(token.calc_operator(a, b));
}
assert(numbers.size() == 1);
return numbers.top();
}
std::string s;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> s;
while (std::cin >> s) {
Expression infix_expression = parse_string(s);
std::clog << infix_expression << std::endl;
Expression postfix_expression = convert_to_postfix(infix_expression);
std::cout << calculate_postfix(postfix_expression) << std::endl;
}
return 0;
}
@@ -0,0 +1,137 @@
#include <cassert>
#include <iostream>
#include <sstream>
#include <variant>
#include <vector>
#include <optional>
#include <stack>
struct Token {
std::variant<char, int> token;
explicit Token(char c) : token(c) {}
explicit Token(int x) : token(x) {}
[[nodiscard]] bool is_operator() const {
return std::holds_alternative<char>(token);
}
[[nodiscard]] char get_operator() const {
if (!is_operator()) { throw std::invalid_argument("Not an operator"); }
return std::get<char>(token);
}
[[nodiscard]] int get_number() const {
if (is_operator()) { throw std::invalid_argument("Is an operator"); }
return std::get<int>(token);
}
bool operator==(char c) const {
if (!is_operator()) { return false; }
return get_operator() == c;
}
bool operator==(int x) const {
if (is_operator()) { return false; }
return get_number() == x;
}
template <typename T>
bool operator!=(T x) {
return !(*this == x);
}
};
using Expression = std::vector<Token>;
Expression parse_string(const std::string& str) {
Expression expression;
std::string temp_token;
for (char c : str) {
if (!(c > '0' && c < '9')) {
if (!temp_token.empty()) {
expression.emplace_back(std::stoi(temp_token));
temp_token.clear();
}
expression.emplace_back(c);
} else {
temp_token += c;
}
}
if (!temp_token.empty()) { expression.emplace_back(std::stoi(temp_token)); }
return expression;
}
void calculate_chunk(std::stack<Token>& undealt_tokens, std::stack<Token>& ready_tokens) {
while (!undealt_tokens.empty() && undealt_tokens.top() != '(') {
ready_tokens.push(undealt_tokens.top());
undealt_tokens.pop();
}
int temp_result = ready_tokens.top().get_number();
ready_tokens.pop();
while (!ready_tokens.empty()) {
char op = ready_tokens.top().get_operator();
ready_tokens.pop();
if (op == '+') {
temp_result += ready_tokens.top().get_number();
} else if (op == '-') {
temp_result -= ready_tokens.top().get_number();
} else {
throw std::logic_error("Unexpected operator token in chunk.");
}
ready_tokens.pop();
}
if (!undealt_tokens.empty()) {
undealt_tokens.pop();
}
undealt_tokens.emplace(temp_result);
}
int calculate(const Expression& expression) {
// 策略:
// 遇到 * / 直接算(本质上是把上一个子 expression 倒栈)
// 遇到右括号 / 结尾开始倒栈
// 对于倒栈,只剩下 +- 需要处理。
std::stack<Token> undealt_tokens, ready_tokens;
std::optional<Token> temp_token;
for (const auto& token : expression) {
if (token == '*' || token == '/') {
temp_token = token;
continue;
}
if (temp_token.has_value()) {
int a = undealt_tokens.top().get_number();
undealt_tokens.pop();
undealt_tokens.emplace(
temp_token == '*' ? a * token.get_number() : a / token.get_number()
);
temp_token.reset();
continue;
}
if (token == ')') {
calculate_chunk(undealt_tokens, ready_tokens);
continue;
}
undealt_tokens.push(token);
}
calculate_chunk(undealt_tokens, ready_tokens);
assert(undealt_tokens.size() == 1);
return undealt_tokens.top().get_number();
}
int n;
std::string s;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int _ = 0; _ < n; _++) {
std::cin >> s;
std::cout << calculate(parse_string(s)) << std::endl;
}
return 0;
}
@@ -0,0 +1,46 @@
#include <iostream>
#include <stack>
#include <vector>
using Operation = std::pair<bool, int>;
int n, idx = 0;
std::stack<int> stack;
std::vector<int> sequence;
std::vector<Operation> operation_log;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
operation_log.reserve(2 * n);
sequence.resize(n);
for (int i = 0; i < n; i++) {
std::cin >> sequence[i];
}
for (int current_num = 1; current_num <= n;) {
while ((stack.empty() || stack.top() != sequence[idx]) && current_num <= n) {
stack.push(current_num);
operation_log.emplace_back(false, current_num);
current_num++;
}
while (!stack.empty() && stack.top() == sequence[idx]) {
stack.pop();
operation_log.emplace_back(true, sequence[idx]);
idx++;
}
}
if (!stack.empty()) {
std::cout << "NO" << std::endl;
} else {
for (const auto [op, x] : operation_log) {
std::cout << (op ? "POP " : "PUSH ") << x << std::endl;
}
}
return 0;
}