码迷,mamicode.com
首页 > 其他好文 > 详细

210 - Concurrency Simulator【模拟、双端队列】

时间:2015-03-14 20:05:30      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

没有什么特别的,就是按照题意进行模拟,代码有点长。。。

#include<cstdio>
#include<queue>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 222;
int n,t[10],q;
int Value[maxn];
/*
    1   a = 1
    2   print
    3   lock
    4   unlock
    5   end
*/
struct Data{
    int kind;
    int aim;
    int value;
    Data(int x,int y,int z = 0){
        kind = x;
        aim  = y;
        value= z;
    }
};
struct Pro{
    vector<Data>q;
    int now;
}arr[maxn];
deque<int>dq;
queue<int>eq;
void debug(){
    for(int i = 0; i < n; i++){
        printf("[%d]\n",i);
        for(int j = 0; j < arr[i].q.size(); j++)
            printf("%d %c %d\n",arr[i].q[j].kind,arr[i].q[j].aim,arr[i].q[j].value);
    }
}
void solve(){
    dq.clear();
    while(!eq.empty()) eq.pop();
    for(int i = 0; i < n; i++)
        dq.push_back(i);
    int islock = 0;
    while(!dq.empty()){
        int time = 0,i;         //记录这个当前程序运行的时间
        int now = dq.front();
        dq.pop_front();
        int have_finish = 0;
        for(; arr[now].now < arr[now].q.size() && time < q; arr[now].now ++ ){
            i = arr[now].now;
            int kind = arr[now].q[i].kind;
            //printf("-> %d %d %d\n",now,i,kind);
            if(kind == 1){
                int aim = arr[now].q[i].aim;
                int value = arr[now].q[i].value;
                Value[aim] = value;
            }
            else if(kind == 2){
                int aim = arr[now].q[i].aim;
                printf("%d: %d\n",now + 1,Value[aim]);
            }
            else if(kind == 3){ //锁住
                if(!islock){    //没有锁住

                }
                else{
                    eq.push(now);
                    have_finish = 1;
                    break;
                }
                islock ++;
            }
            else if(kind == 4){
                islock --;
                if(!eq.empty()){
                    dq.push_front(eq.front());
                    eq.pop();
                }
            }
            else if(kind == 5){
                have_finish = 1;
                break;
            }
            time += t[kind];
        }
        if(!have_finish){
            dq.push_back(now);
        }

    }
}
int main(){
    int T;
    //printf("%d\n",'z');
    scanf("%d",&T);
    while(T--){
        memset(Value,0,sizeof(Value));
        scanf("%d",&n);
        for(int i = 1; i <= 5; i++)
            scanf("%d",&t[i]);
        scanf("%d",&q);
        for(int i = 0; i < n; i++){
            char str[maxn];
            arr[i].now = 0;
            arr[i].q.clear();
            while(true){
                scanf("%s",str);
                if(!strcmp(str,"end")){
                    arr[i].q.push_back(Data(5,0));
                    break;
                }
                else if(!strcmp(str,"print")){
                    scanf("%s",str);
                    arr[i].q.push_back(Data(2,str[0]));
                }
                else if(!strcmp(str,"lock")){
                    arr[i].q.push_back(Data(3,0));
                }
                else if(!strcmp(str,"unlock")){
                    arr[i].q.push_back(Data(4,0));
                }
                else{
                    int value;
                    scanf("%*s%d",&value);
                    arr[i].q.push_back(Data(1,str[0],value));
                }
            }
        }
        //debug();
        solve();
        if(T) printf("\n");
    }
    return 0;
}

210 - Concurrency Simulator【模拟、双端队列】

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/44262273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!