QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#790989#9551. The Emperorucup-team5217#WA 0ms3660kbC++232.5kb2024-11-28 16:20:062024-11-28 16:20:07

Judging History

你现在查看的是最新测评结果

  • [2024-11-28 16:20:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3660kb
  • [2024-11-28 16:20:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll =long long;
#define int long long
int fa[2010];
int siz[2010];
const ll M=998244353;
int get(int x){
    if(fa[x]==x)    return x;
    int t=get(fa[x]);
    siz[x]=siz[x]+siz[fa[x]];
    return fa[x]=t;
}
void merge(int u,int f,ll len){
    fa[u]=f;siz[u]=len;
}
void solve(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)   {fa[i]=i;siz[i]=0;}
    vector<string> s(n+1);
    for(int i=0;i<=n;i++)  getline(cin, s[i]);
    vector<array<int,4>> mp(n+1);
    auto getf=[&](string & s,int pz)->pair<int,int> {
        int ans=0;
        while(pz<(int)s.size()){
            if(s[pz]<='9'&&s[pz]>='0')  ans=ans*10+s[pz++]-'0';
            else break;
        }
        return {ans,pz};
    };
    for(int i=1;i<=n;i++){
        if(s[i].substr(0,4)=="HALT"){
            pair<int,int> w1=getf(s[i],11);
            pair<int,int> w2=getf(s[i],w1.second+6);
            mp[i]={-1,-1,w1.first,w2.first};
        }
        else{
            pair<int,int> w1=getf(s[i],4);
            pair<int,int> w2=getf(s[i],w1.second+6);
            pair<int,int> w3=getf(s[i],w2.second+7);
            pair<int,int> w4=getf(s[i],w3.second+6);
            mp[i]={w1.first,w2.first,w3.first,w4.first};
        }
        // for(int j=0;j<4;j++)   cerr<<mp[i][j]<<' ';
        // cerr<<'\n';
    }
    vector<array<int,3>> stk;//num,ope,pz
    int pz=1;
    vector<bool> flag(n+1);
    int pre=-1;
    ll ans=1;
    while (1){
        int nxt=get(pz);
        if(mp[pz][0]==-1||mp[nxt][0]==-1){
            if(stk.size()==0){
                cout<<(ans%M+M)%M<<'\n';
                return ;
            }   
        }
        // cerr<<"___\n";
        // cerr<<pz<<' '<<pre<<' '<<nxt<<'\n';
        // for(auto [x,y,z]:stk){
        //     cerr<<x<<' '<<y<<' '<<z<<'\n';
        // }
        // cerr<<"___\n";
        ans=(ans+siz[pz]+1)%M;
        if(flag[pz]||flag[nxt])    {cout<<"-1\n";return ;}
        pz=nxt;

        if(pre!=-1)  {flag[pre]=0;pre=-1;}
        if(!stk.empty()&&mp[pz][0]==stk.back()[0]){
            pre=stk.back()[2];
            merge(stk.back()[2],mp[pz][1],(ans-stk.back()[1])%M);
            stk.pop_back();
            pz=mp[pz][1];
        }
        else{
            int num=mp[pz][2];
            stk.push_back({num,ans,pz});
            flag[pz]=1;
            pz=mp[pz][3];
        }
    }
    
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int _ =1;
    // cin>>_;
    while(_--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

1
HALT; PUSH 1 GOTO 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

5
POP 1 GOTO 2; PUSH 1 GOTO 2
HALT; PUSH 1 GOTO 3
POP 1 GOTO 4; PUSH 2 GOTO 4
POP 1 GOTO 2; PUSH 2 GOTO 4
HALT; PUSH 99 GOTO 4

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

1
POP 1 GOTO 1; PUSH 1 GOTO 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3568kb

input:

61
POP 62 GOTO 61; PUSH 30 GOTO 60
POP 1 GOTO 3; PUSH 62 GOTO 61
POP 2 GOTO 61; PUSH 62 GOTO 61
POP 4 GOTO 7; PUSH 2 GOTO 61
POP 62 GOTO 61; PUSH 3 GOTO 4
POP 62 GOTO 61; PUSH 3 GOTO 5
POP 5 GOTO 10; PUSH 3 GOTO 6
POP 62 GOTO 61; PUSH 4 GOTO 7
POP 62 GOTO 61; PUSH 4 GOTO 8
POP 6 GOTO 12; PUSH 4 GOTO...

output:

-1

result:

wrong answer 1st numbers differ - expected: '150994941', found: '-1'