[백준/C++] 16194번_카드 구매하기2

이수진·2022년 1월 18일
0

문제는 다음과 같습니다.

앞에서 보았던 카드 구매하기1번 문제와 모든 원리가 같구요, 앞에서는 최댓값을 구했더라면 이번에는 최솟값을 구하면 됩니다.
원리가 같기에, 따로 설명을 덧붙이지는 않겠습니다!

max -> min으로만 바꾸면 됩니다.

#include <bits/stdc++.h>
using namespace std;

int d[1001]={0, };

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, tmp, res, cnt=0;
    cin>>n;
    cin>>tmp;
    d[1]=tmp;

    for(int i=2; i<=n; i++){
      cin>>tmp;
      d[i]=tmp;
      for(int j=i-1; j>=i/2; j--){
        d[i]=min(d[i], d[j]+d[i-j]);
      }
    }

    cout<<d[n]<<endl;
    
    return 0;
}

2/5 토요일 복습)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int dp[10001]={0, };
    int n, tmp; cin>>n;
    for(int i=1; i<=n; i++){
      cin>>tmp;
      dp[i]=tmp;
    }

    for(int i=2; i<=n; i++){
      for(int j=i-1; j>=1; j--){
        dp[i]=min(dp[i], dp[i-j]+dp[j]);
      }
    }

    cout<<dp[n]<<endl;
    return 0;
}
profile
꾸준히, 열심히, 그리고 잘하자

0개의 댓글