light oj 1212 - Double Ended Queue (双向队列)

本文介绍了一种数据结构——双向队列(Double Ended Queue),并详细解释了其四种基本操作:pushLeft、pushRight、popLeft及popRight。通过示例说明了如何在限定大小的双向队列中执行这些操作,并提供了完整的实现代码。
1212 - Double Ended Queue
Time Limit: 0.5 second(s)Memory Limit: 32 MB

A queue is a data structure based on the principle of 'First In First Out' (FIFO). There are two ends; one end can be used only to insert an item and the other end to remove an item. A Double Ended Queue is a queue where you can insert an item in both sides as well as you can delete an item from either side. There are mainly four operations available to a double ended queue. They are:

1.      pushLeft(): inserts an item to the left end of the queue with the exception that the queue is not full.

2.      pushRight(): inserts an item to the right end of the queue with the exception that the queue is not full.

3.      popLeft(): removes an item from the left end of the queue with the exception that the queue is not empty.

4.      popRight(): removes an item from the right end of the queue with the exception that the queue is not empty.

Now you are given a queue and a list of commands, you have to report the behavior of the queue.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a line containing two integers n, m (1 ≤ n ≤ 10, 1 ≤ m ≤ 100), where n denotes the size of the queue and m denotes the number of commands. Each of the next m lines contains a command which is one of:

pushLeft x        pushes x (-100 ≤ x ≤ 100) in the left end of the queue

pushRight x      pushes x (-100 ≤ x ≤ 100) in the right end of the queue

popLeft             pops an item from the left end of the queue

popRight           pops an item from the right end of the queue


Output

For each case, print the case number in a line. Then for each operation, show its corresponding output as shown in the sample. Be careful about spelling.

Sample Input

Output for Sample Input

1

3 8

pushLeft 1

pushLeft 2

pushRight -1

pushRight 1

popLeft

popRight

popLeft

popRight

Case 1:

Pushed in left: 1

Pushed in left: 2

Pushed in right: -1

The queue is full

Popped from left: 2

Popped from right: -1

Popped from left: 1

The queue is empty

 双向队列,如上图,左右两端都可以既队列又出队列;

解:这题的数据不大,开一个数组,从中间开始模拟队列的进出

代码:

/*
l, r 是加是减 用具体几个数试试就清楚了
*/

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int t,k=1,n,m,x,l,r;
    int a[30];  //队列已经限制小于10
    char str[20];
    cin>>t;
    while(t--)
    {        
        cout<<"Case "<<k++<<":"<<endl;
        int l=r=15;//从中间开始
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            cin>>str;
            if(str[1]=='u')//进队列的情况
            {
                cin>>x;
                if(r-l>=n)                    
                    cout<<"The queue is full"<<endl;
                else
                {
                    if(str[4]=='L')
                    {                        
                        cout<<"Pushed in left: "<<x<<endl;
                        a[l]=x;l--;
                    }
                    else
                    {                        
                        cout<<"Pushed in right: "<<x<<endl;
                        r++;a[r]=x;
                    }
                }
            }
            else//出队列的情况
            {
                if(l==r)                   
                    cout<<"The queue is empty"<<endl;
                else
                {
                    if(str[3]=='L')
                    {                        
                        cout<<"Popped from left: "<<a[l+1]<<endl;
                        l++;
                    }
                    else
                    {
                        cout<<"Popped from right: "<<a[r]<<endl;
                        r--;
                    }
                }
            }
        }
    }
    return 0;
}




内容概要:本文介绍了一个针对电力系统连锁故障传播路径的N-k多阶段双层优化及故障场景筛选模型,该模型基于混合整数线性规划(MILP)方法构建,旨在全面评估电力系统在遭受多重故障时的脆弱性与恢复能力。通过引入故障传播路径的概念,模型能够动态模拟故障在电网中的逐级扩散过程,并结合多阶段优化策略,实现对关键故障场景的有效识别与优先排序。整个框架不仅考虑了初始故障元件的选取,还涵盖了后续因潮流转移引发的级联跳闸行为,从而提升了风险评估的准确性与时效性。该研究已在Matlab平台上完成代码实现,具备良好的可复现性和工程应用价值,适用于提升现代电网的安全防御水平。; 适合人群:电力系统、能源安全及相关领域的科研人员、高校研究生以及从事电网规划与运行管理的工程技术人员。; 使用场景及目标:①用于电力系统安全评估中识别最危险的N-k故障组合;②支撑电网应急预案制定与薄弱环节改造;③作为学术研究中关于级联故障建模与优化求解的教学与验证工具;④服务于智能电网背景下抵御蓄意攻击或极端事件的风险防控决策。; 阅读建议:建议读者结合Matlab代码深入理解模型的数学 formulation 与求解流程,重点关注目标函数设计、约束条件构建及双层优化结构的实现逻辑,同时可通过调整系统参数和故障设定进行仿真对比分析,以掌握不同因素对连锁故障演化的影响规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值