HDU6215【队列+双向链表维护】

本文介绍了一种利用双向链表和队列解决特定序列操作问题的算法实现方案。通过维护序列的位置转移和可能发生的突变点,该算法能够高效地处理一系列数值操作并输出结果。

思路:
队列维护可能发生的突变点。
双向链表维护序列位置转移。
附送一题:HDU6058

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int Maxn = 1e5 + 10;
int pre[Maxn], nex[Maxn];
int a[Maxn], n;
int ans[Maxn], num;
queue<int>q;
bool vis[Maxn];
bool in[Maxn];

void Clear(int x){
    vis[x] = true;
    pre[nex[x]] = pre[x];
    nex[pre[x]] = nex[x];
}

void solve(){
    for(int i=1;i<=n;i++){
        pre[i] = i - 1;
        nex[i] = i + 1;
    }
    pre[n+1] = n;
    nex[0] = 1;
    bool flag = false;
    int tmp = n, Right, pos;
    for(int i=n;i>=1;i--){
        q.push(i);
        vis[i] = false;
        in[i] = true;
    }
    while(!q.empty()){
        Right = q.front();q.pop();in[Right] = false;
        if(vis[Right]) continue;
        pos = Right;
        if(pre[pos]>=1 && a[pos] < a[pre[pos]]){
            if(nex[pos] <= n && !in[nex[pos]])
                q.push(nex[pos]), in[nex[pos]] = true;
            while(pre[pos] >= 1 && a[pos] < a[pre[pos]]){
                Clear(pos);
                pos = pre[pos];
            }
            if(pos >= 1) Clear(pos);
        }
    }
    num = 0;
    for(int i=1;i<=n;++i)
        if(!vis[i]) ans[++num] = a[i];
    printf("%d\n", num);
    for(int i=1;i<=num;i++) printf("%d ", ans[i]);
    puts("");
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i=1;i<=n;i++) scanf("%d", &a[i]);
        if(n == 1){
            puts("1");
            printf("%d\n", a[1]);
            continue;
        }
        solve();
    }
    return 0;
}
HDU 3732 (Queue) 是一个经典的队列操作题目,在这个题目的设定下,你需要处理一系列关于入队、出队的操作,并最终输出特定的结果。这类问题通常会涉及到数据结构中的“队列”这一概念。 ### 题目概述 在 HDU 3732 中,你将面对的是一个标准的队列入栈和出栈的问题变种。它可能会给出一些序列化的指令集,包括: - 入队(push) - 出队(pop) 并且最后询问某些元素的状态或顺序等信息。 ### 解决思路 为了应对这个问题,你可以采用双端队列的数据结构或者两个栈模拟队列的方式来解决问题。以下是具体的步骤: 1. **初始化**:创建所需的辅助变量及容器,如 `queue` 或者一对用于模拟队列行为的栈 (`stackIn`, `stackOut`)。 2. **处理命令流**:遍历输入命令列表,根据不同类型的命令做相应动作: - 对于每一个 push 操作直接添加到指定位置; - 当遇到 pop 命令,则从头部移除元素;如果是用栈实现的话需要特殊处理,比如先全部倒入另一个栈再弹出顶部元素作为当前最先进来的那个值被移走。 3. **生成结果**:按照题目要求整理并返回正确的输出内容。 4. **注意事项** - 确保每次只对有效范围内的索引执行插入/删除操作。 - 考虑边界条件,例如空队列出队等情况下的异常处理。 ### 示例代码片段(Python 实现) ```python from collections import deque def process_queue_commands(commands): queue = deque() for command in commands: if "in" == command[0]: value = int(command.split()[1]) queue.append(value) elif "out" == command and len(queue)>0: print("Pop element:", queue.popleft()) # 示例用法 commands = ["5", "in 8", "in 9", "out"] process_queue_commands(commands[1:]) ``` 请注意实际比赛中给定的具体细节可能有所差异,请参考原题描述进行调整。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值