출처:  http://usaco.org/index.php?page=viewproblem2&cpid=1180


문제 설명

4면체 주사위 3개(A, B, C) 가 있고, 주사위의 각 면의 숫자는 1부터 10 사이의 숫자이다. 두 주사위를 던져서 높은 숫자가 많이 나오는 주사위가 이겼다고 할때, A > B > C > A 혹은 A < B < C < A 가 되는 조합이 있는지 판별하는 문제이다. 입력으로는 주사위 A, B의 숫자들이 주어진다.

입력

3
4 5 6 7 2 4 5 10
2 2 2 2 1 1 1 1
1 1 1 1 2 2 2 2

출력

yes
no
no

 


문제 풀이

주사위 C의 숫자들이 1 ~ 10 이고, 4개의 숫자를 선택할때 가능한 모든 조합 O(N^4)를 해도 10^4 이고, 주사위 2개를 비교해서 편정하는 경우도 4^2 이므로, 전체 계산량은 대략 10^5 정도로 가능하다. 중첩된 Loop 로 모든 경우를 비교하는 알고리즘으로 가능하다. 

프로그램 내용

더보기
...
for(i)
	for(j)
    	for(k)
        	for(l)
            	dice_c = {i, j, k, l}
                if( dice_c >dice_b && dice_b >dice_a && dice_a > dice_c)
                	flag = true; break;
				if( dice_b >dice_c && dice_a >dice_b && dice_c > dice_a)
                	flag = true; break;
if(flag)
	cout << "yes"
else
	cout << "no"

 

'USACO' 카테고리의 다른 글

USACO 2022 January Bronze Problem 1  (0) 2022.02.04

출처:  https://www.acmicpc.net/problem/23970


문제 설명

길이 N 인 두개의 배열 A, B 가 있고, 배열 A 를 버블정렬로 정렬하는 과정에서 배열 B 와 같은 경우가 있는지 확인하는 문제


입력

배열 크기 N 과 두 개의 크기 N 인 정수 배열  A, B

출력

정렬하는 과정에서 같은 경우가 있으면 1 , 없으면 0


입력 예

6
4 6 5 1 3 2
4 1 3 2 5 6

출력 예

1

제약조건

5 <= N <= 1e4, 1 <= a_i, b_i <= 1e9


문제 풀이

Brute force 를 적용하면 bubble sort - O(N^2) 이고, 한번 swap 을 할 때 마다 길이 N 인 두 배열을 비교한다면 추가로 O(N) 이 필요해서, 전체는 O(N^3) 이 되고, 필요시간이 길어져서, 제한시간안에 해결할 수 없다.  ( N=10000 인 경우 O(N^3) 이면 1e12 정도의 계산이 필요하고, 보통 1e9 정도의 계산이 제한시간의 경계이다.)

 

먼저 확인할 것은 주어진 배열이 서로 순서만 다른 배열인지 아니면 내부에 값들이 다른 서로 다른 배열인지 구분해야 한다. 서로 다른 배열이라면, 정렬과정에서 같은 경우가 나올 수는 없다.

 

 

다음으로 순서만 서로 다른 배열이라고 하면 제일 앞에서부터 어디까지가 같은지 비교한 위치를 기록해서 그 이후 값들의 비교를 중심으로 비교하는 과정에서 중복을 줄일 수 있다. 앞에서부터 배열 A 와 배열 B 를 비교해서 일치하는 위치를 m_end로 기억한다. 배열 A를 정렬하는 과정에서 서로 바뀌는 위치를 k 라고 할 때,  k < m_end 인 경우는 계속되는 정렬과정에서 원래 있던 값으로 돌아 갈 수 없으므로 실패가 된다. 또한 m_end < k 인 경우에는 배열 A 와 배열 B 를 비교하면서, 마지막 일치하는 위치 이후의 값들만 비교하고, 계속해서 m_end 값을 갱신해서, 추가적으로 비교해야 하는 경우를 줄 일 수 있다.

프로그램 내용

더보기
..
    if(j < m_end)
    {
        cout << 0;
        return 0;
    }

    for(int k = m_end; k<tN; ++k)
    {
        if(vals[k] == refs[k])
        {
            m_end++;
        }
        else
        {
            break;
        }
    }

    if(m_end == tN)
    {
        cout << 1;
        return 0;
    }
...
...

 

관련된 문제

 

'Baekjoon Online Judge' 카테고리의 다른 글

BOJ 2824 최대공약수  (0) 2021.12.03
BOJ 15686 - 치킨 배달  (0) 2020.03.25
BOJ 2903 - 중앙 이동 알고리즘  (0) 2020.01.28
BOJ 17404 - RGB거리 2  (0) 2019.12.10

출처: 2022 January Bronze Problem 1 ( link )


문제 설명

3X3 charater puzzle

- Green: correct

- Yellow: Mis location

입력 1

COW
SAY
MOO
WIN
THE
IOI

출력

1

1

입력 2

AAA
BBB
CCC
AYY
AAA
ZZZ

출력 

1

2


문제 풀이

문자열 비교를 어떻게 할 것인가 하는 문제이다. 입력으로 주어진 3 x 3 퍼즐을 1 x 9 형태로 바꿔도 문제 풀이에 영향은 없다. char[9] 인 두개의 배열/vector를 만들어서 예상 답안과 실제 답안을 입력하고, 전체를 비교해서 같으면 Green 값에 반영한다.  다음은, 두 답안의 A~Z의 빈도를 확인해서, 잘못된 위치지만 있었는지 확인해서 Yellow 값에 반영한다. 다만, Green 에 반영된 결과를 두번 세지 않도록 해줘야 한다.

프로그램 내용

더보기
// serialization & count frequency 

	for(int i = 0; i<3; ++i)
    {
        string tmp;
        cin >> tmp;
        ans += tmp;
        ans_cnt[tmp[0]-'A']++;
        ans_cnt[tmp[1]-'A']++;
        ans_cnt[tmp[2]-'A']++;
    }
    
    for(int i = 0; i<3; ++i)
    {
        string tmp;
        cin >> tmp;
        gus += tmp;
        gus_cnt[tmp[0]-'A']++;
        gus_cnt[tmp[1]-'A']++;
        gus_cnt[tmp[2]-'A']++;
    }

// cnt_g & cnt_y
    for(int i = 0; i<9; ++i)
    {
        if(ans[i] == gus[i])
            cnt_g++;
    }

    for(int i = 0; i<26; ++i)
    {
        cnt_y += min(ans_cnt[i],gus_cnt[i]);
    }

    cnt_y -= cnt_g;

 

'USACO' 카테고리의 다른 글

USACO 2022 January Bronze Problem 2  (0) 2022.02.21

출처: https://www.acmicpc.net/problem/2824


문제 설명

주어진 수들을 곱해서 만들어지는 큰 수 N, M 의 최대 공약수를 구하는 문제


입력

N을 만드는 곱할 수의 개수(n), 곱할 수들(n_1, ... , n_n) 열, M을 만드는 곱할 수의 개수(m), 곱할 수들 (m_1, ..., m_m)

출력

최대 공약수를 출력한다. 단, 최대 공약수가 1e9 보다 크다면 마지막 9자리만 출력한다. 


입력 예

3

2 3 5

2 5

출력 예

10

제약조건

1 <= N, M <= 1000, 1 <= n_i, m_i <= 1e9


문제 풀이

곱해서 만들어지는 큰 수를  A, B 라고 할때,  prime factorization 을 하면, A = p1^a1 x p2^a2 ... B = p1^b1 x p2^b2 ... 형태로 쓸 수 있고, 같은 소수가 있을 때 그중 거듭제곱이 낮은 것들을 골라서 모두 곱하면 최대 공약수가 된다. 이것을 위해서 곱해서 만들어지는 수들을 각각 소인수분해를 하고 그것을 map<int, int>에 저장한다. 나중에 하나를 기준으로 작은 것들을 골라서 곱하고 크기가 1e9과 비교해서 더 커지지 않는지 확인해서 출력한다. Modulo 연산은 곱셈에 닫혀 있으므로 곱하는 중간에 처리해도 결과 값은 달라지지 않는다.

프로그램 내용

더보기
for(map<int, int>::iterator it = f_n.begin(); it != f_n.end(); ++it)
    {
        int prime;
        int power;
        
        prime = it->first;
        power = min(it->second, f_m[prime]);
        
        // cout << prime << " " << power << "\n";
        
        for(int i = 0; i<power; ++i)
        {
            ans *= prime;
            
            if(ans >= MAX_F)
            {
                ans %= MAX_F;
                flag = true;
            }
        }
    }

 

관련된 문제

 

'Baekjoon Online Judge' 카테고리의 다른 글

BOJ 23970 알고리즘 수업 - 버블 정렬 3  (0) 2022.02.13
BOJ 15686 - 치킨 배달  (0) 2020.03.25
BOJ 2903 - 중앙 이동 알고리즘  (0) 2020.01.28
BOJ 17404 - RGB거리 2  (0) 2019.12.10

Additional Problems

  1. Shortest Subsequence 
  2. Counting Bits 
  3. Swap Game 
  4. Prufer Code 
  5. Acyclic Graph Edges 
  6. Strongly Connected Edges 
  7. Even Outdegree Edges 
  8. Multiplication Table 
  9. Advertisement
  10. Special Substrings 
  11. Permutation Inversions 
  12. Maximum Xor Subarray 
  13. Movie Festival Queries 
  14. Chess Tournament 
  15. Tree Traversals 
  16. Network Renovation 
  17. Graph Girth 
  18. Intersection Points 
  19. Inverse Inversions 
  20. Monotone Subsequences 
  21. String Reorder 
  22. Stack Weights 
  23. Pyramid Array 
  24. Increasing Subsequence II 
  25. String Removals 
  26. Bit Inversions 
  27. Xor Pyramid 
  28. Writing Numbers 
  29. String Transform 
  30. Letter Pair Move Game 
  31. Maximum Building I 
  32. Sorting Methods 
  33. Cyclic Array
  34. List of Sums 
  35. Increasing Array II 
  36. Food Division 
  37. Bit Problem 
  38. Swap Round Sorting 
  39. Binary Subsequences 
  40. Tree Isomorphism I 
  41. Counting Sequences 
  42. Critical Cities 
  43. School Excursion 
  44. Coin Grid 
  45. Robot Path 
  46. Programmers and Artists 
  47. Course Schedule II
  48. Removing Digits II 
  49. Coin Arrangement 
  50. Counting Bishops 
  51. Grid Puzzle I 
  52. Grid Puzzle II 
  53. Empty String 
  54. Grid Paths 
  55. Bit Substrings 
  56. Reversal Sorting 
  57. Counting Reorders 
  58. Book Shop II 
  59. Network Breakdown 
  60. Visiting Cities 
  61. Missing Coin Sum Queries 
  62. Number Grid 
  63. Maximum Building II 
  64. Filling Trominos 
  65. Stick Divisions 
  66. Coding Company 
  67. Flight Route Requests 
  68. Two Stacks Sorting 
  69. Tree Isomorphism II 
  70. Forbidden Cities 
  71. Area of Rectangles 
  72. Grid Completion 
  73. Creating Offices 
  74. Permutations II 
  75. Functional Graph Distribution 
  76. New Flight Routes 
  77. Grid Path Construction 

 

CSES Problem Set 소개 (link)

'CSES' 카테고리의 다른 글

CSES 10. Advanced Techniques  (0) 2021.07.21
CSES 9. Geometry  (0) 2021.07.21
CSES 8. String Algorithms  (0) 2021.07.21
CSES 6. Tree Algorithms  (0) 2021.07.21
CSES 5. Range Queries  (0) 2021.07.21

출처: https://train.usaco.org/usacogate


문제 설명

출처 : IOI'96 - Problem 5

...

The first K characters of S are the prefix of S with length K. 

Write a program which accepts as input a set of primitives and a sequence of constituents and then computes the length of the longest prefix that can be composed from primitives. 

입력

First, the input file contains the list (length 1..200) of primitives (length 1..10) expressed as a series of space-separated strings of uppercase characters on one or more lines.

The list of primitives is terminated by a line that contains nothing more than a period (`.').

No primitive appears twice in the list.

 

Then, the input file contains a sequence S (length 1..200,000) expressed as one or more lines, none of which exceeds 76 letters in length.

 

The "newlines" (line terminators) are not part of the string S.

출력

A single line containing an integer that is the length of the longest prefix that can be composed from the set P.

입력 예

A AB BA CA BBC
.
ABABACABAABC

출력 예

11


문제 풀이

만들려고 하는 문자열 길이의 앞에서 부터 출발해서, 부분 문자열이 존재하는지 검사한다. 단순 double loop를 적용해도 부분 문자열의 수를 N, 만들려고 하는 문자열의 길이를 M이라고 하면 O(NM) 정도로 해결가능해진다.

프로그램 내용

더보기
... 
	// string target;
	// vector<string> primitives;
    
	dp[0]=1;
	for(int i=0;i< target.size();i++) 
	{
        if(dp[i])
        { 
            for(int j=0;j<primivites.size();j++) 
            {
                if( primivies[j]==target.substr(i,primitives[j].size()) ) 
                {
                    dp[i+primivies[j].size()] = 1;
                }
            }
        }
    }
    ...

 

Chapter 2. Bigger Challenges ( link

'USACO Training' 카테고리의 다른 글

Problem 2.2.3 Preface Numbering  (0) 2021.01.21
Problem 2.1.3 The Castle  (0) 2021.01.07
Problem 2.1.7 Hamming Codes  (0) 2021.01.06
Problem 1.2.5 Greedy Gift Givers - 2  (0) 2021.01.03
Problem 2.1.6 Healthy Holsteins  (0) 2020.02.11

출처: https://train.usaco.org/usacogate


문제 설명

Given N (1 <= N < 3,500), the number of pages in the preface of a book, calculate and print the number of I's, V's, etc. (in order from lowest to highest) required to typeset all the page numbers (in Roman numerals) from 1 through N. 

Do not print letters that do not appear in the page numbers specified.

입력

A single line containing the integer N.

출력

The output lines specify, in ascending order of Roman numeral letters, the letter, a single space,
and the number of times that letter appears on preface page numbers. Stop printing letter totals
after printing the highest value letter used to form preface numbers in the specified set.

입력 예

5

출력 예

I 7
V 2

( I, II, III, IV, V) -> I: 1(1)+2(2)+3(3)+1(4) = 7, V : 1(4) + 1(5) = 2


문제 풀이

1 부터 입력받은 숫자까지 Roman 숫자로 표현할때 각 숫자들이 몇번 나오는지 세는 프로그램을 작성한다.

Roman 숫자표현에서 나오는 문자는 7가지 이다.( I - 1, V - 5, X - 10, L - 50, C - 100 , D - 500, M - 1000) 

이런 7개 문자들이 몇번 나오는지 unordered_map<char, int> 형태로 기록한다.

프로그램 내용

더보기

 

...
	string ones[9] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    string tens[9] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    string hundreds[9] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    string thousands[3] = {"M", "MM", "MMM"};
...
    unordered_map<char, int> m;
    for (int i = 1; i <= N; i++) {
        int num = i;
        
        if (num / 1000 > 0) {
            count(m, thousands[num / 1000 - 1]);
            num %= 1000;
        }
        if (num / 100 > 0) {
            count(m, hundreds[num / 100 - 1]);
            num %= 100;
        }
        if (num / 10 > 0) {
            count(m, tens[num / 10 - 1]);
            num %= 10;
        }
        if (num > 0) {
            count(m, ones[num - 1]);
        }
    }
    if (m.find('I') != m.end()) fout << "I " << m['I'] << endl;
    if (m.find('V') != m.end()) fout << "V " << m['V'] << endl;
    if (m.find('X') != m.end()) fout << "X " << m['X'] << endl;
    if (m.find('L') != m.end()) fout << "L " << m['L'] << endl;
    if (m.find('C') != m.end()) fout << "C " << m['C'] << endl;
    if (m.find('D') != m.end()) fout << "D " << m['D'] << endl;
    if (m.find('M') != m.end()) fout << "M " << m['M'] << endl;
...

 

Chapter 2. Bigger Challenges ( link )

'USACO Training' 카테고리의 다른 글

Section 2.3.1 Prob The Longest Prefix  (0) 2021.04.01
Problem 2.1.3 The Castle  (0) 2021.01.07
Problem 2.1.7 Hamming Codes  (0) 2021.01.06
Problem 1.2.5 Greedy Gift Givers - 2  (0) 2021.01.03
Problem 2.1.6 Healthy Holsteins  (0) 2020.02.11

출처: https://train.usaco.org/usacogate


문제 설명

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

 

입력

Line 1: Two space-separated integers: M and N
Line 2..: M x N integers, several per line.

출력

Line 1: The number of rooms the castle has.
Line 2: The size of the largest room
Line 3: The size of the largest room creatable by removing one wall
Line 4: The single wall to remove to make the largest room possible

입력 예

7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

출력 예

5
9
16
4 1 E


문제 풀이

IOI'94 - Day 1 의 문제이다. N x M maze에서 연결된 방들을 채워가는 Flood fill 알고리즘을 적용하면, 전체 연결된 방을 알 수 있다. 한방에서 연결된 방을 찾아가는 방법은 DFS가 적용된다.  N x M maze를 다루는 연습이 필요한 문제이다. 벽을 제거하면서 값을 비교할때는 항상 계산이 끝난 다음 원래 값으로 변경시키는 과정을 적용해야 한다.

프로그램 내용

더보기
void fill_cells(int y, int x, int& direction)
{
    if(visited[y][x]) return;
    visited[y][x] = 1;
    before[y][x] = roomNumber;
    direction++;

    if(!(cells[y][x] & 1)) fill_cells(y,x-1, direction); /// Connected Direction: W
    if(!(cells[y][x] & 2)) fill_cells(y-1,x, direction); /// Connected Direction: N
    if(!(cells[y][x] & 4)) fill_cells(y,x+1, direction); /// Connected Direction: E
    if(!(cells[y][x] & 8)) fill_cells(y+1,x, direction); /// Connected Direction: S
};
...

한 방에 도착하면 연결된 벽을 확인하고, 벽으로 막혀 있지 않으면 그 방까지 연결한다.

 /// visit every rooms
    for(int y=1; y <N+1; ++y)
    {
        for(int x=1; x<M+1;++x)
        {
            if(!visited[y][x])
            {
                roomNumber++;
                int tn=0;
                fill_cells(y,x,tn);
                room[roomNumber] = tn;
                if( tn > maxn ) maxn = tn;
            }
        }
    }

    /// remove wall check
    for(int j=1;j<=M;j+=1)
    {
        for(int i=N;i>=1;i-=1)
        {
            if(cells[i][j]&2)if(before[i-1][j] && before[i-1][j]!=before[i][j])
            {
                if( room[before[i-1][j]] + room[before[i][j]] > max2n )
                {
                    max2n = room[before[i-1][j]] + room[before[i][j]];
                    wx = i,wy = j,wd = 'N';
                }
            }

            if(cells[i][j]&4)if(before[i][j+1] && before[i][j+1]!=before[i][j])
            {
                if( room[before[i][j+1]] + room[before[i][j]] > max2n )
                {
                    max2n = room[before[i][j+1]] + room[before[i][j]];
                    wx = i,wy = j,wd = 'E';
                }
            }
        }
    }

전체 방에 대해서 벽을 제거해서 결과값이 최대가 될때마다 제거하는 벽을 기록하는 방법을 적용한다.

 

Chapter 2. Bigger Challenges ( link )

'USACO Training' 카테고리의 다른 글

Section 2.3.1 Prob The Longest Prefix  (0) 2021.04.01
Problem 2.2.3 Preface Numbering  (0) 2021.01.21
Problem 2.1.7 Hamming Codes  (0) 2021.01.06
Problem 1.2.5 Greedy Gift Givers - 2  (0) 2021.01.03
Problem 2.1.6 Healthy Holsteins  (0) 2020.02.11

출처: https://train.usaco.org/usacogate


문제 설명

Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8),
such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each
of the other codewords.

입력

N, B, D on a single line

출력

N codewords, sorted, in decimal, ten per line.

입력 예

16 7 3

출력 예

0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127


문제 풀이

Hamming Distance 계산하는 방법에 대한 문제이다. 각각의 bit 값을 비교하기 위해서, 2로 계속 나누고 달라질때마다 distance를 증가시키는 방법으로 계산한다. 0 부터 숫자를 키워가면서 distance > D인 경우들을 계속 추가시켜서 기록하고, 출력할때 10개 단위로 출력한다.

프로그램 내용

더보기
int getHammingDistance(int a, int b)
{
    int ret=0;

    while( a> 0 || b>0)
    {
        if( a%2 != b%2)
            ret++;
        a /= 2;
        b /= 2;
    }
    return ret;
};

...

  ans.push_back(0);
    /// check hamming distance > D, put result
    for(int i=0;i < maxB ;i++)
    {
        bool ok= true;

        /// compare hamming distance with last code
        for(int j=0;j!=ans.size();j++)
        {
            /// distance < D
            if( getHammingDistance(i,ans[j]) < D )
            {
                ok = false;
                break;
            }
        }
        /// distance >= D
        if(ok)
        {
            ans.push_back(i);
        }

        /// Max output number
        if(ans.size() >= N)
        {
                break;
        }
    }

 

Chapter 2. Bigger Challenges ( link )

'USACO Training' 카테고리의 다른 글

Problem 2.2.3 Preface Numbering  (0) 2021.01.21
Problem 2.1.3 The Castle  (0) 2021.01.07
Problem 1.2.5 Greedy Gift Givers - 2  (0) 2021.01.03
Problem 2.1.6 Healthy Holsteins  (0) 2020.02.11
USACO Training - Chapter 6. Contest Practice  (0) 2020.01.01

+ Recent posts