울타리 잘라내기 문제 질문입니다.

  • jucie15
    jucie15
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void index_switching(int maxIndex , int &L , int &R, int n);
    int max_index(int *h , int n);
    /*
    
    오답 : 문제에 있는 예제는 모두 정확한 답이 나왔으나 오답.
    사용한 알고리즘 가장 큰 높이를 기준 양옆에서 가장 높은 판자를 선택해 나가며 최대 넓이 계산.
    (넓이 계산시 선택한 판자들중 가장 낮은 높이의 판자 * 선택한 판자의 갯수(너비))
    
    7 1 5 9 6 7 3
          9         = 9
          6 6       = 12
          6 6 6     = 18
        5 5 5 5     = 20
        3 3 3 3 3   = 15
      1 1 1 1 1 1   = 6
    1 1 1 1 1 1 1   = 7
    */
    int main()
    {
        int c;//test case
        int n;//판자의 갯수
        int *h;//각 판자의 높이
        int maxIndex = 0;
        int L,R;
        int cnt = 1;//너비를 계산
        int min = 9999;//가장 작은 높이를 메모
        int maxArea = 0;//가장 큰 넓이를 메모
        int flag = 0;//필요없는 수
    
        scanf("%d",&c);
    
        while(c--)
        {
            scanf("%d",&n);
            h = (int *) malloc (sizeof(int) * n);
    
            for(int i = 0 ; i < n ; i++)
            {
                scanf("%d",&h[i]);
            }//for
    
            cnt = 1;//cnt 초기화
            maxIndex = max_index(h,n);//maxIndex 초기화
            min = h[maxIndex];//min 초기화
            maxArea = h[maxIndex];//maxArea 초기화
            index_switching(maxIndex , L , R , n);//L,R 초기화
    
            for(int i = 0 ; i < n ; i++)
            {
                if(L != -1 && R != -1)//왼쪽 오른쪽 판자가 모두 있을 경우
                {
                    if( h[L] <= h[R])
                    {
                        cnt++;
                        if(min > h[R])
                        {
                            min = h[R];
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        else
                        {
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        index_switching(R , flag , R , n);  
                    }
    
                    else if( h[L] > h[R])
                    {
                        cnt++;
                        if(min > h[L])
                        {
                            min = h[L];
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        else
                        {
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        index_switching(L , L , flag , n);  
                    }
                }//if
                else if(L != -1 && R == -1)//판자가 왼쪽에만 남아있을 경우
                {
                    cnt++;
                    if(min > h[L])
                        {
                            min = h[L];
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        else
                        {
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        index_switching(L , L , flag , n);  
                }//else if
                else// 판자가 오른쪽에만 남아있을 경우
                {
                        cnt++;
                        if(min > h[R])
                        {
                            min = h[R];
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        else
                        {
                            if(maxArea < (cnt * min))
                                maxArea = cnt * min;
                        }
                        index_switching(R , flag , R , n);  
                }//else
            }//for
            printf("%d\n",maxArea);
    
        }//while
    
        return 0;
    }
    int max_index(int *h , int n)//배열안에서 가장 큰값의 인덱스를 구함
    {
        int max = 0;
        for(int i = 1; i < n ; i++)
        {
            if(h[max] < h[i])
            {
                max = i;
            }
        }
        return max;
    }
    
    void index_switching(int maxIndex , int &L , int &R, int n)//LEFT 와 RIGHT의 인덱스를 조정한다.
    {
        if(maxIndex - 1 >= 0)   
            L = maxIndex - 1;
        else
            L = -1;
        if(maxIndex + 1 < n)
            R = maxIndex + 1;
        else
            R = -1;
    }
    

    일반적인 예는 다 되는것같은데. special case가 있는지. 뭐가 문제인지 좀 알려주실수 있을까요!?!


    10년 전
2개의 댓글이 있습니다.
  • JongMan
    JongMan

    1 9 1 8 8 8 8 8 8 이런 경우에 잘되나요? :)


    10년 전 link
  • jucie15
    jucie15

    아 제 알고리즘이 잘못됬네요...
    특별할 것도 없는 예인데 보이는것만 너무 신경쓰고 있었네요 감사합니다.ㅎㅎ


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