Advertisement
Korotkodul

21-22_N5_v3

Mar 24th, 2023 (edited)
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. #define ob push_back
  10. #define pii pair <int, int>
  11.  
  12. bool is_dead = 0;
  13. pii ans = {-1, -1};
  14. vector <char> resource(101, '.');
  15. map <char, int> process;
  16.  
  17. bool sh = 1;
  18.  
  19.  
  20. void look() {
  21.     cout << "resource\n";
  22.     for (int i = 0; i <= 100; ++i) {
  23.         if (resource[i] != '.') {
  24.             cout << i << " " << resource[i] << "\n";
  25.         }
  26.     }
  27.     cout << "process\n";
  28.     for (auto p: process) {
  29.         if (p.second != -1) {
  30.             cout << p.first << ' ' << p.second << "\n";
  31.         }
  32.     }
  33.     cout << "\n";
  34. }
  35.  
  36. bool dead(int x, int y) {
  37.     if (resource[x] == '.') {
  38.         return 0;
  39.     }
  40.     if (resource[y] == '.') {
  41.         return 0;
  42.     }
  43.     char prox = process[x];
  44.     char proy = process[y];
  45.     return process[proy] == x && process[prox] == y;
  46. }
  47.  
  48. void check() {
  49.     for (int i = 0; i <= 100; ++i) {
  50.         for (int j = 0; j <= 100; ++j) {
  51.             if (i == j) {
  52.                 continue;
  53.             }
  54.             if (dead(i,j) && !is_dead) {
  55.                 is_dead = 1;
  56.                 ans = {i, j};
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     for (int i = 0; i < 26; ++i) {
  65.         char k = (int)'A' + i;
  66.         process[k] = -1;
  67.     }
  68.     while (1) {
  69.         if (sh) {
  70.             look();
  71.         }
  72.         check();
  73.         string restr;
  74.         cin >> restr;
  75.         int re;
  76.         char pro, act;
  77.         if (restr == ".") {
  78.             break;
  79.         }
  80.         re = stoi(restr);
  81.         cin >> pro >> act;
  82.         if (act == 'U') {
  83.             process[pro] = -1;
  84.             for (int i = 0; i <= 100; ++i) {
  85.                 if (resource[i] == pro) {
  86.                     resource[i] = '.';
  87.                 }
  88.             }
  89.             continue;
  90.         }
  91.  
  92.         if (process[pro] == -1) {
  93.             process[pro] = re;
  94.             continue;
  95.         }
  96.         resource[re] = pro;
  97.     }
  98.  
  99.     if (is_dead) {
  100.         cout << "DEADLOCK\n";
  101.         cout << ans.first << " " << ans.second << "\n";
  102.         exit(0);
  103.     }
  104.  
  105.     cout << "NO DEADLOCK\n";
  106.     int locked = 0;
  107.     for (int i = 0; i <= 100; ++i)  {
  108.         if (resource[i] != '.') {
  109.             locked++;
  110.         }
  111.     }
  112.     cout << locked << "\n";
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement