MAXTACK 문제 관련하여 질문드립니다.

  • sven
    sven

    MAXTACK

    input 관련 assert 에서 계속 failure 가 떠서, input 만 받는 solve2 함수를 만들어서 돌려봤는데도 같은 결과가 뜹니다.
    문제를 완전히 잘못 이해한 것인지... ㅜㅜ 조언 부탁드립니다.
    혹시 몰라서 원래 코드를 그대로 첨부합니다만, solve() 함수로는 들어가는 일이 없을 것입니다.

    #include <iostream>
    #include <vector>
    #include <cassert>
    #include <queue>
    #include <set>
    #include <stack>
    #include <algorithm>
    
    #define EACH(x) for(set<pair <int, bool> >::iterator it=x.begin(); it!=x.end(); ++it)
    
    using namespace std;
    
    void solve();
    void solve2()
    {
      int N;
      cin >> N;
      int pushed = 0;
      for(int i=0; i<N; i++)
      {
        string temp;
        cin >> temp;
        cout << temp << endl;
        assert(temp == "push" || temp == "add" || temp == "subtract" || temp == "negate");
        if(temp == "push")
          pushed++;
      }
      for(int i=0; i<pushed; i++)
      {
        int temp;
        cin >> temp;
      }
    
    }
    int main()
    {
      int T;
      cin >> T;
      while(T--)
        solve2();
    }
    
    void solve()
    {
      string D[4] = {"push", "add", "subtract", "negate"};
      stack <set <pair <int, bool> > > A;
      int N;
      int pushed = 0;
      int summed = 0;
      cin >> N;
      for(int i=0; i<N; i++)
      {
        string temp;
        cin >> temp;
    //    cout << "pushed : " << pushed << " and summed : " << summed << " and size of A : " << A.size() << endl;
    //    assert(pushed - summed == A.size());
        if(!temp.compare(D[0]))
        {
          set <pair <int, bool> > temp;
          temp.insert(make_pair(pushed, true));
          A.push(temp);
          pushed++;
        }
        else if(!temp.compare(D[1]))
        {
          summed++;
          set <pair <int, bool> > x = A.top();
          A.pop();
          set <pair <int, bool> > y = A.top();
          A.pop();
          set <pair <int, bool> > z;
    //      set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(z, z.begin()));
          EACH(x) z.insert(make_pair((*it).first, (*it).second));
          EACH(y) z.insert(make_pair((*it).first, (*it).second));
          A.push(z);
        }
        else if(!temp.compare(D[2]))
        {
          summed++;
          set <pair <int, bool> > x = A.top();
          A.pop();
          set <pair <int, bool> > y = A.top();
          A.pop();
          set <pair <int, bool> > z;
          EACH(x) z.insert(*it);
          EACH(y) z.insert(make_pair((*it).first, !(*it).second));
          A.push(z);
        }
        else if(!temp.compare(D[3]))
        {
          set <pair <int, bool> > x = A.top();
          A.pop();
          set <pair <int, bool> > z; 
          EACH(x) z.insert(make_pair((*it).first, !(*it).second));
          A.push(z);
        }
        else
          assert(false);
      }
      //cout << "pushed : " << pushed << " and summed : " << summed << " and size of A : " << A.size() << endl;
      //EACH(A.top()) cout << (*it).first << " " << (*it).second << endl;
    //  assert(pushed - summed == A.size());
    //  assert(A.size() == 1);
    
      vector <int> B;
      for(int i=0; i<pushed; i++)
      {
        int temp;
        cin >> temp;
        B.push_back(temp);
      }
      sort(B.begin(), B.end());
      int b_index = 0;
      int ans[pushed];
      int sum = 0;
      EACH(A.top())
        if(!(*it).second)
        {
          ans[(*it).first] = B[b_index];
          sum -= B[b_index];
          b_index++;
        }
      EACH(A.top())
        if((*it).second)
        {
          ans[(*it).first] = B[b_index];
          sum += B[b_index];
          b_index++;
        }
    
      cout << sum << endl;
      for(int i=0; i<pushed; i++)
        cout << ans[i] << " ";
      cout << endl;
    
    }
    

    10년 전
3개의 댓글이 있습니다.
  • Being
    Being

    이거 문제에 꽤 중요한 힌트인데 똑같은 이유로 틀리셨던 다른 분이 있으신데요... istream은 한 번 입력이 적절하지 않아 고장나면 그 뒤로 입력을 더는 받질 않습니다.


    10년 전 link
  • sven
    sven

    cin 에서 scanf 로 바꾸니 동작하네요. 왜 그런지는 아직도 잘 모르겠습니다 ㅜㅜ


    10년 전 link
  • wookayin
    wookayin

    그 다른분이 바로 접니다ㅠㅠ

    cin은 overflow/잘못된 입력(int인데 string이 온다거나..) 나면 그 다음부터 고장나서 그 이후에 동작이 다 이상해진다고 하네요.

    아마 이 문제 입력이 unsigned int 타입이라 overflow가 나면 그 이후로 잘못된 동작을 했을거에요[..] 물론 scanf도 signed int로 받으면 overflow로 답은 틀리겠죠.

    위 레퍼런스를 참고해보세요 :)


    10년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.