Skip to content

Pro4_WA_60

原始文件为 CPP 代码,本文是转换后的 Markdown 文件。

#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <stack>

using namespace std;

const int maxn = 10005;
const int diffint = (1 << 20);
vector<int> process[maxn];
int pos[maxn];
bool isDead[maxn];

int main()
{
    ios::sync_with_stdio(false);
    freopen("in5.txt","r",stdin);
    int T,n; cin>>T;cin>>n; cin.get();

    for (int i = 0; i < T; ++i)
    {
        memset(pos,0,sizeof(pos));
        memset(isDead,false,sizeof(isDead));
        int count = 0;
        string line;
        for (int i = 0; i < n; ++i)
        {
            process[i].clear();
            getline(cin,line);
            stringstream ss(line);
            string item;

            while(getline(ss,item,' '))
            {
                if (item[0] == 'S' || item[0] == 's')
                    process[i].push_back(stoi(item.erase(0,1)));
                else if (item[0] == 'R' || item[0] == 'r')
                    process[i].push_back(stoi(item.erase(0,1)) + diffint);  

                ++count;
            }
        }

        int index = 0;
        int waitnum = 0;
        bool flag = true;
        while (count > 0)
        {
            if (waitnum == n)
            {
                flag = false;
                break;
            }
            int cur = pos[index];
            if (cur >= process[index].size())
            {
                index = (index+1)%n;
                continue;
            }
            int tempindex = process[index][cur];

            // cout << index << "--" << cur << "--" << process[index][cur] << endl;

            if(process[index][cur] < diffint)
            {
                // S 
                int tempcur = pos[tempindex];
                // cout << tempindex << "--" << tempcur << endl;

                if (tempcur >= process[tempindex].size())
                {
                    flag = false;
                    break;
                }
                if(process[tempindex][tempcur] == diffint+index)
                {
                    if (isDead[tempindex]) --waitnum; 
                    count = count - 2;
                    ++pos[tempindex]; ++pos[index];
                    // index = tempindex;
                }else
                {
                    isDead[index] = true;
                    ++waitnum;
                    index = (index + 1) % n;
                }
            }else
            {
                // R
                tempindex = tempindex - diffint;
                int tempcur = pos[tempindex];
                if (tempcur >= process[tempindex].size())
                {
                    flag = false;
                    break;
                }
                if (process[tempindex][tempcur] == index)
                {
                    ++pos[index]; ++pos[tempindex];
                    count = count - 2;
                    if (isDead[tempindex]) --waitnum;
                    // index = tempindex;
                }else
                {
                    isDead[index] = true;
                    index = (index + 1) % n;
                    ++waitnum;
                }
            }
        }

        int ans = (flag) ? 0 : 1;
        cout << ans << endl;
    }
}