AtCoder Beginner Contest 153 E - Crested Ibis vs Monster

Crested Ibis vs Monster
做法使用一个dp[i]代表从0到i所需要最少的cost

 dp[target] = min(dp[target],dp[j] + b[i]);

解法如下

#include
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
#define CINSPEED ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
#define fi first
#define se second
#define pb push_back

ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){ return a * b / gcd(a,b);}


const int N = 10010,M = 1000010;
int h,n;
int dp[N],a[N],b[N];
void solved()
{
    cin >> h >> n;
    
    for(int i = 0;i < n;i++)cin >> a[i] >> b[i];
    memset(dp,0x3f,sizeof dp);
    
    dp[0] = 0;
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < h;j++)    
        {
            int target = j + a[i];
            target = target >= h ? h : target;
            dp[target] = min(dp[target],dp[j] + b[i]);
        }
    }
    cout << dp[h];
}

int main()
{
    CINSPEED;
    solved();
#ifdef LOCAL
    system("pause");
#endif
    return 0;
}

你可能感兴趣的:(AtCoder,dp)