zoukankan      html  css  js  c++  java
  • UVA 210 Concurrency Simulator

    题目链接:https://vjudge.net/problem/UVA-210

    题目大意

      一共有 T 组案例。

      对于每组案例,你的任务是模拟n个程序(按输入顺序编号1~n)的并行执行。每个程序包含不超过25条语句。

      格式一共是5种:赋值(var=constant),打印(print var),lock,unlock,end,耗时分别为$t_1,t_2,t_3,t_4,t_5$​。

      变量用一个小写字母表示,初始时为0,为所有并行程序共有,且它的值始终保持在[0,100]内,所以一个程序对某一个变量的赋值会影响到另外一个程序。

      每个时刻只能是一个程序处于运行状态,其他程序处于等待状态。运行状态之中的的程序每次最多分配Q个单位时间,一旦在未执行完某个程序的某个指令时超过分配时间,则执行完那个指令后,这个程序会被插入等待队列,然后从其的队首取出一个程序继续执行。而初始的等待队列为按照输入程序排入。

      但是由于lock和unlock命令的出现,这个顺序会被改变。

      lock的作用是申请对所有变量的独占访问,unlock则是解除对所有变量的独占访问,且它们一定成对出现。当一个程序已经对所有的变量独占访问后,其他程序若试图执行lock,无论其是否耗尽分配时间,都会被放在一个阻止队列的尾部,且当那个程序解除的时候,则会从阻止队列的头部的程序进入等待队列的头部。

      现在给出$n,t_1,t_2,t_3,t_4,t_5,Q$以及n个程序,你需要输出所有print命令执行输出的值。

      每组案例的输出结果之间空一行。

      翻译搬运自洛谷。

    分析

      并行程序模拟,不难,细心即可。

    代码如下

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3  
      4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
      5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
      6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
      7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
      8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
      9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     12  
     13 #define pr(x) cout << #x << " = " << x << "  "
     14 #define prln(x) cout << #x << " = " << x << endl
     15  
     16 #define LOWBIT(x) ((x)&(-x))
     17  
     18 #define ALL(x) x.begin(),x.end()
     19 #define INS(x) inserter(x,x.begin())
     20  
     21 #define ms0(a) memset(a,0,sizeof(a))
     22 #define msI(a) memset(a,inf,sizeof(a))
     23 #define msM(a) memset(a,-1,sizeof(a))
     24 
     25 #define MP make_pair
     26 #define PB push_back
     27 #define ft first
     28 #define sd second
     29  
     30 template<typename T1, typename T2>
     31 istream &operator>>(istream &in, pair<T1, T2> &p) {
     32     in >> p.first >> p.second;
     33     return in;
     34 }
     35  
     36 template<typename T>
     37 istream &operator>>(istream &in, vector<T> &v) {
     38     for (auto &x: v)
     39         in >> x;
     40     return in;
     41 }
     42  
     43 template<typename T1, typename T2>
     44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     45     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     46     return out;
     47 }
     48 
     49 inline int gc(){
     50     static const int BUF = 1e7;
     51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     52     
     53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     54     return *bg++;
     55 } 
     56 
     57 inline int ri(){
     58     int x = 0, f = 1, c = gc();
     59     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     61     return x*f;
     62 }
     63  
     64 typedef long long LL;
     65 typedef unsigned long long uLL;
     66 typedef pair< double, double > PDD;
     67 typedef pair< int, int > PII;
     68 typedef pair< int, PII > PIPII;
     69 typedef pair< string, int > PSI;
     70 typedef pair< int, PSI > PIPSI;
     71 typedef set< int > SI;
     72 typedef vector< int > VI;
     73 typedef vector< VI > VVI;
     74 typedef vector< PII > VPII;
     75 typedef map< int, int > MII;
     76 typedef map< string, int > MSI;
     77 typedef multimap< int, int > MMII;
     78 typedef unordered_map< int, int > uMII;
     79 typedef pair< LL, LL > PLL;
     80 typedef vector< LL > VL;
     81 typedef vector< VL > VVL;
     82 typedef priority_queue< int > PQIMax;
     83 typedef priority_queue< int, VI, greater< int > > PQIMin;
     84 const double EPS = 1e-10;
     85 const LL inf = 0x7fffffff;
     86 const LL infLL = 0x7fffffffffffffffLL;
     87 const LL mod = 1e9 + 7;
     88 const int maxN = 1e5 + 7;
     89 const LL ONE = 1;
     90 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
     91 const LL oddBits = 0x5555555555555555;
     92 
     93 struct Instruction{
     94     queue< PIPSI > ins;
     95 }IS[37];
     96 int len;
     97 
     98 int n, t[6], Q, T; 
     99 deque< int > waitQ; // 等待队列 
    100 queue< int > blockQ; // 阻塞队列 
    101 MSI vars; // 存储变量映射 
    102 bool islock; // 当前是否有锁 
    103 
    104 void solve() {
    105     while(!waitQ.empty()) {
    106         int nowM = waitQ.front();
    107         waitQ.pop_front();
    108         
    109         int tmp = 0;
    110         queue< PIPSI > &tmpIS = IS[nowM].ins;  // 当前指令队列 
    111         while(tmp < Q && !tmpIS.empty()) {
    112             PIPSI inst = tmpIS.front();
    113             tmp += t[inst.ft];
    114             
    115             switch(inst.ft) {
    116                 case 1:{
    117                     vars[inst.sd.ft] = inst.sd.sd;
    118                     break;
    119                 }
    120                 case 2:{
    121                     cout << nowM << ": " << vars[inst.sd.ft] << endl;
    122                     break;
    123                 }
    124                 case 3:{
    125                     if(islock) {
    126                         blockQ.push(nowM);
    127                         tmp = inf;
    128                     }
    129                     else islock = true;
    130                     break;
    131                 }
    132                 case 4:{
    133                     islock = false;
    134                     if(!blockQ.empty()) {
    135                         waitQ.push_front(blockQ.front());
    136                         blockQ.pop();
    137                     }
    138                     break;
    139                 }
    140                 case 5: break;
    141             }
    142             if(tmp != inf) tmpIS.pop();    
    143         }
    144         if(!tmpIS.empty() && tmp != inf) waitQ.PB(nowM);
    145     }
    146 }
    147 
    148 int main(){
    149     //freopen("MyOutput.txt","w",stdout);
    150     INIT();
    151     cin >> T;
    152     while(T--) {
    153         cin >> n;
    154         For(i, 1, 5) cin >> t[i];
    155         cin >> Q;
    156         len = 0;
    157         vars.clear();
    158         
    159         For(i, 1, n) {
    160             string tmp;
    161             queue< PIPSI > &tmpIS = IS[++len].ins;
    162             waitQ.PB(len);
    163             
    164             while(cin >> tmp) {
    165                 if(tmp == "end") {
    166                     tmpIS.push(MP(5, MP("", -1)));
    167                     break;
    168                 }
    169                 else if(tmp == "print") {
    170                     cin >> tmp;
    171                     tmpIS.push(MP(2, MP(tmp, -1)));
    172                 }
    173                 else if(tmp == "lock") tmpIS.push(MP(3, MP("", -1)));
    174                 else if(tmp == "unlock") tmpIS.push(MP(4, MP("", -1)));
    175                 else {
    176                     string tt;
    177                     int x;
    178                     cin >> tt >> x;
    179                     tmpIS.push(MP(1, MP(tmp, x)));
    180                 }
    181             }    
    182         }
    183         solve();
    184         if(T) cout << endl;
    185     }
    186     return 0;
    187 }
    View Code
  • 相关阅读:
    spring自定义标签
    shell脚本实战
    redis使用场景
    了解并安装Nginx
    查看jar包依赖树
    从一道索引数据结构面试题看B树、B+树
    11条sql技巧
    or/in/union与索引优化
    动态规划
    实现快速迭代的引擎设计
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/10967065.html
Copyright © 2011-2022 走看看