some programs!!!

This commit is contained in:
rogerwang2008 committed 2026-10-09 10:21:51 +08:00
1 parent 71f1022dea
commit ca6daa582c
43 files changed
+1452 -381

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
#include <iostream>
constexpr int MAX_N = 1e6 + 1;
int n, ans = 0;
int values[MAX_N], sums[MAX_N], min_sums[2][MAX_N];
void init_sums() {
sums[0] = values[0];
for (int i = 1; i < n; i++) {
sums[i] = sums[i - 1] + values[i];
}
}
void init_min_sums() {
min_sums[0][0] = sums[0];
for (int i = 1; i < n; i++) {
min_sums[0][i] = std::min(min_sums[0][i - 1], sums[i]);
}
min_sums[1][n - 1] = sums[n - 1];
for (int i = n - 2; i >= 0; i--) {
min_sums[1][i] = std::min(min_sums[1][i + 1], sums[i]);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> values[i];
}
init_sums();
init_min_sums();
for (int i = 0; i < n - 1; i++) {
int delta1 = sums[i];
int delta2 = sums[n - 1] - sums[i];
if (min_sums[0][i] + delta2 >= 0 && min_sums[1][i + 1] - delta1 >= 0) {
std::clog << i << std::endl;
ans++;
}
}
bool flag = false;
for (int i = 0; i < n; i++) {
if (sums[i] < 0) {
flag = true;
break;
}
}
if (!flag) {
std::clog << n - 1 << std::endl;
ans++;
}
std::cout << ans << std::endl;
return 0;
}
+117
View File
@@ -0,0 +1,117 @@
#include <iostream>
#include <vector>
constexpr int MAX_G = 301;
template <typename NodeT>
class LinkedList {
public:
NodeT* head = new NodeT();
NodeT* tail = head;
NodeT* push_front(int val) {
auto* node = new NodeT{.val = val};
push_front(node);
return node;
}
void push_front(NodeT* node) {
tail->next = node;
tail = node;
}
void push_back(NodeT* node) {
node->next = head->next;
head->next = node;
}
int pop_back() {
NodeT* prev_node = head->next;
head->next = prev_node->next;
if (tail == prev_node) { tail = head; }
int val = prev_node->val;
delete prev_node;
return val;
}
NodeT* back() {
return head->next;
}
bool empty() {
return head == tail;
}
};
struct Node {
int val = -1;
Node* next = nullptr;
// Node* prev = nullptr;
};
struct AdvancedNode : Node {
using Node::Node;
int group{};
AdvancedNode* next = nullptr;
LinkedList<Node> children;
};
class AdvancedLinkedList : public LinkedList<AdvancedNode> {
public:
// Node* head = new Node();
AdvancedNode* father_nodes[MAX_G] = {};
AdvancedNode* push_front(int val, int grp) {
auto* node = new AdvancedNode();
node->val = val, node->group = grp;
if (father_nodes[grp] == nullptr) {
father_nodes[grp] = node;
LinkedList::push_front(node);
} else {
father_nodes[grp]->children.push_front(node);
}
return node;
}
int pop_back() {
if (back()->children.empty()) {
father_nodes[back()->group] = nullptr;
return LinkedList::pop_back();
}
int current_val = back()->val;
back()->val = back()->children.pop_back();
return current_val;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, groups_amount;
std::vector<int> element_groups;
AdvancedLinkedList list;
std::cin >> n >> groups_amount;
element_groups.resize(n);
for (int i = 0; i < n; i++) {
std::cin >> element_groups[i];
}
std::cin >> n;
std::string op;
int elem_id;
for (int _ = 0; _ < n; _++) {
std::cin >> op;
if (op == "push") {
std::cin >> elem_id;
list.push_front(elem_id, element_groups[elem_id]);
} else {
std::cout << list.pop_back() << std::endl;
}
}
return 0;
}
@@ -0,0 +1,96 @@
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
constexpr int INF = 0x3f3f3f3f;
template <typename T>
class QueueWithMinMax : public std::queue<T> {
protected:
std::deque<T> min_queue;
std::deque<T> max_queue;
public:
using std::queue<T>::queue;
void push(T x) {
while (!min_queue.empty() && min_queue.back() > x) {
min_queue.pop_back();
}
min_queue.push_back(x);
while (!max_queue.empty() && max_queue.back() < x) {
max_queue.pop_back();
}
max_queue.push_back(x);
std::queue<T>::push(x);
}
void pop() {
if (!min_queue.empty() && min_queue.front() == std::queue<T>::front()) {
min_queue.pop_front();
}
if (!max_queue.empty() && max_queue.front() == std::queue<T>::front()) {
max_queue.pop_front();
}
std::queue<T>::pop();
}
T get_max() {
return max_queue.front();
}
T get_min() {
return min_queue.front();
}
auto get_difference() -> decltype(get_max() - get_min()) {
if (std::queue<T>::empty()) { return -1; }
return get_max() - get_min();
}
};
using Range = std::pair<int, int>;
using Coord = std::pair<int, int>;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, target_diff;
std::vector<Coord> coords;
std::vector<int> positions;
std::map<int, Range> moments;
// std::vector<Range> moments;
std::cin >> n >> target_diff;
coords.resize(n);
for (int i = 0; i < n; i++) {
// std::cin >> positions[i] >> temp;
std::cin >> coords[i].first >> coords[i].second;
}
std::sort(coords.begin(), coords.end());
int ans = +INF, p1 = 0, p2 = 0;
QueueWithMinMax<int> queue;
queue.push(coords[0].second);
while (p2 < n) {
if (queue.get_difference() < target_diff) {
p2++;
if (p2 < n) { queue.push(coords[p2].second); }
} else {
if (coords[p1].first == coords[p2].first) {
ans = 0;
break;
}
ans = std::min(ans, coords[p2].first - coords[p1].first);
p1++;
queue.pop();
}
}
if (ans == +INF) { ans = -1; }
std::cout << ans << std::endl;
return 0;
}
@@ -0,0 +1,87 @@
#include <algorithm>
#include <iostream>
#include <queue>
constexpr int INF = 0x3f3f3f3f;
template <typename T>
class QueueWithMinMax : public std::queue<T> {
protected:
std::deque<T> min_queue;
std::deque<T> max_queue;
public:
using std::queue<T>::queue;
void push(T x) {
while (!min_queue.empty() && min_queue.back() > x) {
min_queue.pop_back();
}
min_queue.push_back(x);
while (!max_queue.empty() && max_queue.back() < x) {
max_queue.pop_back();
}
max_queue.push_back(x);
std::queue<T>::push(x);
}
void pop() {
if (!min_queue.empty() && min_queue.front() == std::queue<T>::front()) {
min_queue.pop_front();
}
if (!max_queue.empty() && max_queue.front() == std::queue<T>::front()) {
max_queue.pop_front();
}
std::queue<T>::pop();
}
T get_max() {
return max_queue.front();
}
T get_min() {
return min_queue.front();
}
auto get_difference() -> decltype(get_max() - get_min()) {
if (std::queue<T>::empty()) { return -1; }
return get_max() - get_min();
}
};
using Range = std::pair<int, int>;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, target_diff;
std::vector<int> positions;
std::vector<Range> moments;
std::cin >> n >> target_diff;
positions.resize(n), moments.assign(n, Range(INF, -INF));
for (int i = 0; i < n; i++) {
int temp;
std::cin >> positions[i] >> temp;
// moments[i].first = std::min(moments[i].first, temp);
}
int ans = +INF, p1 = 0, p2 = 0;
QueueWithMinMax<int> queue;
queue.push(moments[0]);
while (p2 < n) {
if (queue.get_difference() < target_diff) {
p2++;
if (p2 < n) { queue.push(moments[p2]); }
} else {
ans = std::min(ans, p2 - p1 + 1);
p1++;
queue.pop();
}
}
std::cout << ans << std::endl;
return 0;
}