Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ближайшее число
- #include <iostream>
- #include <fstream>
- struct Node {
- int value;
- Node* next;
- };
- void push(Node** head, int value) {
- Node* new_node = new Node[sizeof(Node)];
- new_node->next = *head;
- new_node->value = value;
- *head = new_node;
- }
- void pop(Node** head) {
- if ((*head) != nullptr)
- *head = (*head)->next;
- }
- int top(Node* head) {
- if (head != nullptr)
- return head->value;
- }
- int isEmpty(Node** head){
- if (*head == nullptr)
- return 1;
- return 0;
- }
- int main() {
- std::ifstream fin("input.txt");
- std::ofstream fout("output.txt");
- int N;
- fin >> N;
- int a[N];
- for (int i = 0; i < N; ++i){
- fin >> a[i];
- }
- Node *stack = nullptr;
- Node *index = nullptr;
- push(&stack, a[0]);
- push(&index, 0);
- for (int i = 1; i < N; ++i){
- while (!isEmpty(&stack) && top(stack) < a[i]){
- a[top(index)] = a[i];
- pop(&stack);
- pop(&index);
- }
- push(&stack, a[i]);
- push(&index, i);
- }
- for (int i = 0; i < N; ++i)
- fout << a[i] << " ";
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment