ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 189-A, *1300
    코드포스 2021. 7. 27. 21:20
     

    Problem - 189A - Codeforces

     

    codeforces.com

    문제

    • 세 정수 $a,\ b,\ c(1 \leq a,\ b,\ c \leq 4000)$가 주어진다
    • 길이가 $n(1 \leq n \leq 4000)$인 끈을 길이 $a,\ b,\ c$로만 자를 때 나오는 조각의 최대 개수는?

     

    $O(n)$

    $DP_{i}$를 길이가 $i$인 끈을 길이 $a,\ b,\ c$로만 자를 때 나오는 조각의 최대 개수라고 하자. 그러면
    $$\mathrm{DP_{i}=max(DP_{i-a},\ DP_{i-b},\ DP_{i-c})+1},\ \mathrm{Not\ all\ of\ DP_{i-a},\ Dp_{i-b},\ Dp_{i-c}=0}$$

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
     
    int n, a, b, c;
    int dp[40001] {};
     
    int Maxval(int a, int b, int c)
    {
        int maxval = a;
        if(maxval < b) maxval = b;
        if(maxval < c) maxval = c;
        return maxval;
    }
     
    int Dp(int ind)
    {
        if(ind >= 0return dp[ind];
        else return 0;
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        cin >> n >> a >> b >> c;
        dp[a] = 1;
        dp[b] = 1;
        dp[c] = 1;
        for(int i = 1; i <= n; i++) {
            if(Dp(i-a) or Dp(i-b) or Dp(i-c)) dp[i] = Maxval(Dp(i-a), Dp(i-b), Dp(i-c)) + 1;
        }
     
        cout << dp[n] << endl;
     
        return 0;

     

     

    $O(n^{2})$

    $$ax+by+cz=n,\ (1 \leq x,\ y,\ z \leq n)$$
    을 만족하는 $x+y+z$의 최대값을 찾으면 된다
    $$cz=n-ax-by$$
    식을 위와 같이 정리하고 가능한 모든 $x,\ y$에 대해 $z$가 음이아닌 정수인지 판단한다
    $z$가 음이 아닌 정수인 모든 경우에 대해 $x+y+z$의 최대값을 구한다 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int n, a, b, c;
        cin >> n >> a >> b >> c;
        int cz;
        int maxval = 0;
        for(int x = 0; x <= 4000; x++){
            for(int y = 0; y <= 4000; y++){
                cz = n - a*- b*y;
                if(cz >= 0 && cz%c == 0){
                    maxval = max(maxval, x+y+cz/c);
                }
            }
        }
     
        cout << maxval << endl;
     
        return 0;

    '코드포스' 카테고리의 다른 글

    459-B, *1300  (0) 2021.07.28
    451-B, *1300  (0) 2021.07.28
    230-B, *1300  (0) 2021.07.27
    4-C, *1300  (0) 2021.07.25
    1360-C, *1100  (0) 2021.07.25

    댓글

Designed by Tistory.