CANDLESTICK 질문입니다.

  • porcosedol
    porcosedol

    오답이 발생하는데 제가 생각해봤던 테스트케이스들로는 문제점을 못 찾아서 테스트케이스 추천을 부탁드리려고 질문드립니다.

    #include <iostream>
    #include <utility>
    #include <vector>
    
    using namespace std;
    
    
    void solve() {
        int num;
        cin >> num;
    
        vector<pair<int, int>> p;
        for (int i = 0; i < num; i++) {
            int first, second;
            cin >> first >> second;
            p.push_back(make_pair(first, second));
        }
    
        int max_size = 0;
        pair<int, int> last_minmax;
        vector<pair<int, int>> w;
        for (int i = 0; i < p.size(); i++) {
            int max_low = p.front().first;
            int min_high = p.front().second;
            int last_min = 0;
            int last_max = 0;
            int last_pos = 0;
            for (int j = i; j < p.size(); j++) {
                if (p.at(j).first >= last_max || p.at(j).second <= last_min) {
                    max_low = p.at(j).first;
                    min_high = p.at(j).second;
                    last_pos = j - i;
                }
                else {
                    if (p.at(j).first > max_low)
                        max_low = p.at(j).first;
                    if (p.at(j).second < min_high)
                        min_high = p.at(j).second;
                }
    
    
                w.push_back(p.at(j));
                last_min = p.at(j).first;
                last_max = p.at(j).second;
                auto size = (min_high - max_low) * (w.size() - last_pos);
                if (size > max_size)
                    max_size = size;
                //for (auto k : w)
                //cout << k.first << " - " << k.second << " ";
                //cout <<" size = "<< size << endl;
                //if (j == p.size() - 1)
                //  w.clear();
            }
            w.clear();
        }
        cout << max_size << endl;
    }
    
    
    
    int main() {
    
        int test_num;
        cin >> test_num;
        while (test_num)    {
            solve();
            test_num--;
        }
        return 0;
    }
    

    9년 전
1개의 댓글이 있습니다.
  • JongMan
    JongMan

    알고리즘을 간단하게라도 설명해 주시는 편이 답변을 쉽게 받으실 수 있을 겁니다.


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