紫书章六例题一(双端口队列)

题目比较难懂,然后之后的就还好。双端口队列
deque q;
q.push_back();
q.push_front();
q.pop_front();
q.pop_back();
q.front();

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <sstream>
using namespace std;
const int maxn = 1005;
vector<string> v[maxn];
queue< int >  qz;
deque < int > qd;
int p[maxn];//进行到第几步
int val[30];//变量的值
int t[10],Q;
int LOCK=0;
void run(int i)
{
    int total=Q;
    while(total>0)//注意是大于0!!!
    {
        string cur=v[i][p[i]];
        if(cur[2]=='=')
        {
            total-=t[0];
            string a=cur.substr(4);
            stringstream ss;
            ss<<a;
            int len;
            ss>>len;
            val[cur[0]-'a']=len;
        }
        else if(cur[2]=='i')
        {
            total-=t[1];
            printf("%d: %d\n",i,val[cur[6]-'a']);
        }
        else if(cur[2]=='c')
        {
            total-=t[2];
            if(!LOCK) {LOCK=1;}
            else {qz.push(i);return ;}
        }
        else if(cur[2]=='l')
        {
            total-=t[3];
            LOCK=0;
            if(!qz.empty()){
            int tt=qz.front();
            qz.pop();
            qd.push_front(tt);
            }
        }
      /*  else if(cur[2]=='d')
        {
            total-=t[4];
            flag=1;
            continue;
        }*/
        else return ;//如果为end的话直接return,还不用考虑那么多
        p[i]++;
    }
  qd.push_back(i);
}
int main()
{
   // freopen ("E:\\input.txt","r",stdin);
    int case1;
    scanf("%d",&case1);
    while(case1--){
       // while(!qz.empty()) qz.pop();
        //for(int i=0;i<maxn;i++)v[i].clear();
        memset(val,0,sizeof(val));
     //   memset(t,0,sizeof(t));
        memset(p,0,sizeof(p));
        int n;
        scanf("%d",&n);
          for(int j=0;j<5;j++)
                scanf("%d",&t[j]);
        scanf("%d",&Q);
        for(int i=1;i<=n;i++)
        {
            v[i].clear();
            string s;
            while(getline(cin,s))
            {
                if(s=="") continue;
                v[i].push_back(s);
                if(s=="end")
                    break;
            }
            qd.push_back(i);
        }
        while(!qd.empty())
        {
            int temp=qd.front();
            qd.pop_front();
            run(temp);
        }
        if(case1) printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值