'ACM'에 해당되는 글 6건

  1. 2008/02/17 귀차니스트 The Skyline Problem (2)
  2. 2008/02/17 귀차니스트 Ecological Bin Packing
  3. 2008/02/17 귀차니스트 LCD Display (3)
  4. 2008/02/17 귀차니스트 3n+1 Problem
  5. 2008/02/17 귀차니스트 The Blocks Problem

3n+1 Problem

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36
  2.  의   경   : 처음으로 나오는 쉬운 기초적인 문제입니다. 하지만 체크해야될 조건이 있습니다. 바로 입력 숫자의 크기가 서로 바뀔 수 있다는 점이죠.
    입력 방식이 10 20과 같이 입력될 때에는 괜찮으나 20 10과 같이 입력될 때에는 체크를 꼭 해주어야 합니다.
  3. 소스
    100.c (Language : c)
    1. #include <stdio.h>
    2. int Calculation( int number );
    3. int main( int argc, char **argv )
    4. {
    5.     int StInput, EnInput;
    6.     int StartNum, EndNum, RetValue;
    7.     int i, MaxCycle;
    8.     while( scanf( "%d %d", &StInput, &EnInput ) == 2 ) {
    9.         if( StInput > EnInput ) {
    10.             StartNum = EnInput;
    11.             EndNum = StInput;
    12.         }
    13.         else {
    14.             StartNum = StInput;
    15.             EndNum = EnInput;
    16.         }
    17.         MaxCycle = 0;
    18.         for( i = StartNum; i <= EndNum; i++ ) {
    19.             RetValue = Calculation( i );
    20.             if( RetValue > MaxCycle )
    21.                 MaxCycle = RetValue;
    22.         }
    23.         printf( "%d %d %d\n", StInput, EnInput, MaxCycle );
    24.     }
    25.     return 0;
    26. }
    27. int Calculation( int number )
    28. {
    29.     int CycleLength;
    30.     for( CycleLength = 1; number != 1; CycleLength++ ) {
    31.         if( number == 1 )
    32.             break;
    33.         if( number % 2 == 1 ) {
    34.             number = number * 3 + 1;
    35.         }
    36.         else {
    37.             number = number >> 1;
    38.         }
    39.     }
    40.     return CycleLength;
    41. }


크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
The Skyline Problem (2)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30
TAG , , ,

댓글을 달아 주세요

The Blocks Problem

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37
  2.  의   견   : 명령어 파싱에 대한 연습을 하는 문제라고 생각합니다. 한 가지 언어 특성상 Pointer를 이용한 List 형식으로 코드를 작성할 때 구현에 있어 난이도가 조금 있을 것입니다. 그리하여 STL을 부분적으로 사용했었고
    , 아마 지금 재작성 한다면 table을 비롯하여 모든 부분을 STL으로 작성할 것 같습니다.
  3. 소스
    101.cpp (Language : cpp)
    1. #include <iostream>
    2. #include <deque>
    3. #include <string>
    4. using namespace std;
    5. deque< int > *table;
    6. int *link;
    7. int BlockNum;
    8. void returning( int index );
    9. void moving( int index1, int index2 );
    10. void piling( int index1, int index2 );
    11. void print();
    12. int main( int argc, char **argv )
    13. {
    14.     cin >> BlockNum;
    15.     if( 0 < BlockNum && BlockNum < 25 ) {
    16.         table = new deque< int >[ BlockNum ];
    17.         link = new int[ BlockNum ];
    18.         for( int i = 0; i < BlockNum; i++ ) {
    19.             link[ i ] = i;
    20.             table[ i ].push_back( i );
    21.         }
    22.         string cmd1;
    23.         int a;
    24.         string cmd2;
    25.         int b;
    26.         while( true ) {
    27.             cin >> cmd1;
    28.             if( cmd1 == "quit" ) {
    29.                 print();
    30.                 break;
    31.             }
    32.             cin >> a >> cmd2 >> b;
    33.             if( a != b && link[ a ] != link[ b ] && 0 <= a && a < BlockNum && 0 <= b && b < BlockNum ) {
    34.                 if( cmd1 == "move" ) {
    35.                     if( cmd2 == "onto" ) {
    36.                         // move A onto B
    37.                         returning( a );
    38.                         returning( b );
    39.                         moving( a, b );
    40.                     }
    41.                     else if( cmd2 == "over" ) {
    42.                         // move A over B
    43.                         returning( a );
    44.                         moving( a, b );
    45.                     }
    46.                 }
    47.                 else if( cmd1 == "pile" ) {
    48.                     if( cmd2 == "onto" ) {
    49.                         // pile A onto B
    50.                         returning( b );
    51.                         piling( a, b );
    52.                     }
    53.                     else if( cmd2 == "over" ) {
    54.                         // pile A over B
    55.                         piling( a, b );
    56.                     }
    57.                 }
    58.             }
    59.         }
    60.         delete [] table;
    61.         delete [] link;
    62.     }
    63.     return 0;
    64. }
    65. void returning( int index )
    66. {
    67.     int BlockIndex = link[ index ];
    68.     while( table[ BlockIndex ].back() != index ) {
    69.         int Block = table[ BlockIndex ].back();
    70.         table[ Block ].push_back( Block );
    71.         table[ BlockIndex ].pop_back();
    72.         link[ Block ] = Block;
    73.     }
    74. }
    75. void moving( int index1, int index2 )
    76. {
    77.     int a = link[ index1 ];
    78.     int b = link[ index2 ];
    79.     table[ b ].push_back( table[ a ].back() );
    80.     table[ a ].pop_back();
    81.     link[ index1 ] = link[ index2 ];
    82. }
    83. void piling( int index1, int index2 )
    84. {
    85.     deque< int > temp;
    86.     int BlockIndex1 = link[ index1 ];
    87.     while( table[ BlockIndex1 ].back() != index1 ) {
    88.         temp.push_back( table[ BlockIndex1 ].back() );
    89.         table[ BlockIndex1 ].pop_back();
    90.     }
    91.     temp.push_back( table[ BlockIndex1 ].back() );
    92.     table[ BlockIndex1 ].pop_back();
    93.     int BlockIndex2 = link[ index2 ];
    94.     while( !temp.empty() ) {
    95.         int linkTemp = temp.back();
    96.         temp.pop_back();
    97.         table[ BlockIndex2 ].push_back( linkTemp );
    98.         link[ linkTemp ] = link[ index2 ];
    99.     }
    100. }
    101. void print()
    102. {
    103.     deque< int >::iterator prt;
    104.     for( int i = 0; i < BlockNum; i++ ) {
    105.         cout << i << ":";
    106.         prt = table[ i ].begin();
    107.         while( prt != table[ i ].end() ) {
    108.             cout << " " << *prt;
    109.             prt++;
    110.         }
    111.         cout << endl;
    112.     }
    113. }
크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

3n+1 Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
The Skyline Problem (2)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30
TAG , , , ,

댓글을 달아 주세요

Maximum Sum

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44
  2.  의   견   : Dynamic Programming을 배웠고, 사용한다면 쉬운 문제, 하지만 단순히 for 루프를 이용하여 6중 for 루프를 사용한다면 time exceed를 면할 수 없을 것입니다.
    DP 방법은 배열을 사용할 때 [ 시작 x 인덱스 ][ 시작 y 인덱스 ][ 끝 x 인덱스 ][ 끝 y 인덱스 ]로 의미를 부여하여 대상 값들을 계산 후 업데이트하고 재사용합니다.
  3. 소스
    108.cpp (Language : cpp)
    1. #include <iostream>
    2. using namespace std;
    3. short Matrix[ 101 ][ 101 ][ 101 ][ 101 ] = { 0, };
    4. int main( int argc, char **argv )
    5. {
    6.     int MaxSum = -999999;
    7.     int size;
    8.     cin >> size;
    9.     for( int i = 1; i <= size; i++ ) {
    10.         for( int j = 1; j <= size; j++ ) {
    11.             cin >> Matrix[ i ][ j ][ i ][ j ];
    12.         }
    13.     }
    14.     for( int StartI = 1; StartI <= size; StartI++ ) {
    15.         for( int StartJ = 1; StartJ <= size; StartJ++ ) {
    16.             for( int EndI = StartI; EndI <= size; EndI++ ) {
    17.                 for( int EndJ = StartJ; EndJ <= size; EndJ++ ) {
    18.                     short RectangleSum = Matrix[ StartI ][ StartJ ][ EndI ][ EndJ - 1 ];
    19.                     short LineSum = Matrix[ StartI ][ EndJ ][ EndI - 1 ][ EndJ ];
    20.                     short NewLineSum = LineSum + Matrix[ EndI ][ EndJ ][ EndI ][ EndJ ];
    21.                     Matrix[ StartI ][ EndJ ][ EndI ][ EndJ ] = NewLineSum;
    22.                     short NewRectangleSum = RectangleSum + NewLineSum;
    23.                     Matrix[ StartI ][ StartJ ][ EndI ][ EndJ ] = NewRectangleSum;
    24.                     if( MaxSum < NewRectangleSum )
    25.                         MaxSum = NewRectangleSum;
    26.                 }
    27.             }
    28.         }
    29.     }
    30.     cout << MaxSum;
    31.     return 0;
    32. }

크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
The Skyline Problem (2)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요

The Skyline Problem

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=41
  2.  의   견   : 일일이 하나의 경우에 대해서 앞의 모든 경우를 검사하는 것이 아님을 가르쳐주는 문제. 방법은 배열을 선언, 입력이 들어올 때마다 배열을 범위로 선택 값을 갱신합니다. 출력시에는 다른 지점의 인덱스만 출력하면 됩니다.
  3. 소스
    105.cpp (Language : cpp)
    1. #include <iostream>
    2. short Skyline[ 10001 ] = { 0, };
    3. void InsertHeight( int Left, int Right, int Height )
    4. {
    5.     for( int i = Left; i < Right; ++i ) {
    6.         if( Skyline[ i ] < Height )
    7.             Skyline[ i ] = Height;
    8.     }
    9. }
    10. void PrintHeight()
    11. {
    12.     int LastHeight = 0;
    13.     int CurrentHeight;
    14.     bool IsFirst = true;
    15.     for( int i = 0; i < 10001; ++i ) {
    16.         CurrentHeight = Skyline[ i ];
    17.         if( CurrentHeight != LastHeight ) {
    18.             if( IsFirst )
    19.                 IsFirst = false;
    20.             else {
    21.                 std::cout << " ";
    22.             }
    23.             std::cout << i << " " << CurrentHeight;
    24.             LastHeight = CurrentHeight;
    25.         }
    26.     }
    27.     std::cout << std::endl;
    28. }
    29. int main( int argc, char **argv )
    30. {
    31.     int Left, Height, Right;
    32.     while( std::cin >> Left >> Height >> Right ) {
    33.         InsertHeight( Left, Right, Height );
    34.     }
    35.     PrintHeight();
    36.     return 0;
    37. }



크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
Ecological Bin Packing (0)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요

  1. alex 2008/08/19 23:39  댓글주소  수정/삭제  댓글쓰기

    그냥 using namespace std 쓰시지 ...
    큰 작업도 아니고 uva 문제 푸는 정도면
    유도리 있게 코딩하시는거도 좋다고 생각합니다.

    • 귀차니스트 2008/09/21 21:21  댓글주소  수정/삭제

      아 ㅋㅋ 물론 진짜 ACM 대회를 나가게 된다면 시간이 중요하니 그럴수 있겠지만.. 평상시 일 때는 원칙을 되도록 지키고 싶어서 그렇습니다^^.

Ecological Bin Packing

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=38
  2.  의   견   : 나올 수 있는 경우의 수는 3! 개 입니다. 즉, 경우의 수가 적으므로 6가지 경우에 대한 측정값들을 수동으로 계산하여 출력할 수 있습니다. 하지만, 일반적으로 계산을 해보고자 하는 마음으로 priority_queue와 string을 짝으로 조합하여 구현을 했습니다.
  3. 소스
    103.cpp (Language : cpp)
    1. #include <iostream>
    2. #include <queue>
    3. #include <string>
    4. struct SortNode {
    5.     int Total;
    6.     std::string OutputStr;
    7. };
    8. bool operator < ( const SortNode &Left, const SortNode &Right )
    9. {
    10.     size_t LeftLength = Left.OutputStr.length();
    11.     size_t RightLength = Right.OutputStr.length();
    12.     if( ( Left.Total == Right.Total ) && ( LeftLength == RightLength ) )    {
    13.         for( int i = 0; i < 3; i++ )    {
    14.             char LeftChar = Left.OutputStr[ i ];
    15.             char RightChar = Right.OutputStr[ i ];
    16.             if( LeftChar == RightChar )
    17.                 continue;
    18.             else if( LeftChar > RightChar )
    19.                 return true;
    20.             else
    21.                 return false;
    22.         }
    23.     }
    24.     return ( Left.Total > Right.Total );
    25. }
    26. int main( int argc, char **argv )
    27. {
    28.     unsigned int Bottle[ 9 ] = { 0, };
    29.     while( std::cin >> Bottle[ 0 ] >> Bottle[ 1 ] >> Bottle[ 2 ] >> Bottle[ 3 ] >> Bottle[ 4 ] >> Bottle[ 5 ] >> Bottle[ 6 ] >> Bottle[ 7 ] >> Bottle[ 8 ] )    {
    30.         std::priority_queue< SortNode > BackTracking;
    31.         SortNode Temp;
    32.         Temp.Total = 0;
    33.         BackTracking.push( Temp );
    34.         while( true )   {
    35.             SortNode ProcessNode = BackTracking.top();
    36.             BackTracking.pop();
    37.             size_t ProcessStrLength = ProcessNode.OutputStr.length();
    38.             if( ProcessStrLength == 3 ) {
    39.                 std::cout << ProcessNode.OutputStr << " " << ProcessNode.Total << std::endl;
    40.                 break;
    41.             }
    42.             ProcessStrLength *= 3;
    43.             if( ProcessNode.OutputStr.find( "C" ) == UINT_MAX ) {
    44.                 SortNode CNode( ProcessNode );
    45.                 CNode.Total += ( Bottle[ ProcessStrLength ] + Bottle[ ProcessStrLength + 1 ] );
    46.                 CNode.OutputStr += "C";
    47.                 BackTracking.push( CNode );
    48.             }
    49.             if( ProcessNode.OutputStr.find( "G" ) == UINT_MAX ) {
    50.                 SortNode GNode( ProcessNode );
    51.                 GNode.Total += ( Bottle[ ProcessStrLength ] + Bottle[ ProcessStrLength + 2 ] );
    52.                 GNode.OutputStr += "G";
    53.                 BackTracking.push( GNode );
    54.             }
    55.             if( ProcessNode.OutputStr.find( "B" ) == UINT_MAX ) {
    56.                 SortNode BNode( ProcessNode );
    57.                 BNode.Total += ( Bottle[ ProcessStrLength + 1 ] + Bottle[ ProcessStrLength + 2 ] );
    58.                 BNode.OutputStr += "B";
    59.                 BackTracking.push( BNode );
    60.             }
    61.         }
    62.     }
    63.     return 0;
    64. }



크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
The Skyline Problem (2)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요