【雙端佇列】例題6-1 UVa 210

CN_swords發表於2017-04-30


/*
演算法競賽入門 LRJ 例題6-1(UVa 210)Concurrency Simulator 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
typedef long long LL;
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int INF=0x3f3f3f3f;
const int N = 10010;

int mp[10];
int val[30];
int id[N];
string res[N];
deque<int> Q;
deque<int> stop;
int n,q;
int change(string x)
{
    int sum = 0;
    for(int i = 3; i < x.size(); i++)
    {
        if(x[i] >= '0' && x[i] <= '9')
            sum = sum*10+(x[i]-'0');
    }
    return sum;
}
int lock;
void solve(int x)
{
    int time = q;
    while(time > 0)
    {
        string now = res[id[x]];
        //cout << " now " << now << endl;
        if(now[2] == '=')
        {
            val[now[0]-'a'] = change(now);
            time -= mp[0];
        }
        else if(now[2] == 'i')
        {
            printf("%d: %d\n",x,val[now[6]-'a']);
            time -= mp[1];
        }
        else if(now[2] == 'c')
        {
            if(lock == 1)
            {
                stop.push_back(x);
                return ;
            }
            lock = 1;
            time -= mp[2];
        }
        else if(now[2] == 'l')
        {
            if(!stop.empty())
            {
                int temp = stop.front();
                stop.pop_front();
                Q.push_front(temp);
            }
            lock = 0;
            time -= mp[3];
        }
        else
        {
            //End = 1;
            time -= mp[4];
            return ;
        }
        id[x]++;
    }
    Q.push_back(x);
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d%d%d%d",&n,&mp[0],&mp[1],&mp[2],&mp[3],&mp[4],&q);
        memset(val,0,sizeof(val));
        Q.clear();
        stop.clear();
        getchar();
        int num = 0;
        for(int i = 1; i <= n; i++)
        {
            Q.push_back(i);
            getline(cin,res[num++]);
            id[i] = num-1;
            while(res[num-1] != "end")
                getline(cin,res[num++]);
        }
        lock = 0;
        while(!Q.empty())
        {
            int x = Q.front();
            Q.pop_front();
            solve(x);
        }
        if(T)
            puts("");
    }
    return 0;
}

相關文章