210-concurrency Simulator "Analog, dual-ended queue"

Source: Internet
Author: User

Nothing special, just follow the test instructions simulation, the code is a bit long ...

#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 UNL    Ock 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];d eque<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;        Log this current program run time 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) {//Lock if (!islock) {//Not locked} 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 "Analog, dual-ended queue"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.