码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Equal"

时间:2015-07-07 07:03:50      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

First I was stuck at how to represent state of "add all except i".. but after checking editorial, it is simply inverted Coin Change problem..

技术分享
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MOD 1000000007
#define MAX_VAL 2000

int main() 
{
    //    Coin Change Problem
    vector<int> dp(MAX_VAL, MOD);
    dp[0] = 0;
    for(int i = 0; i < MAX_VAL; i ++)
    {
        if((i + 1) < MAX_VAL)
        {
            dp[i + 1] = std::min(dp[i + 1], dp[i] + 1);
        }
        if((i + 2) < MAX_VAL)
        {
            dp[i + 2] = std::min(dp[i + 2], dp[i] + 1);
        }
        if((i + 5) < MAX_VAL)
        {
            dp[i + 5] = std::min(dp[i + 5], dp[i] + 1);
        }
    }

    int t; cin >> t;
    while(t--)
    {
        //    Get input
        int n; cin >> n;
        vector<int> in(n);
        for(int i = 0; i < n; i ++)
            cin >> in[i];

        int mn = *std::min_element(in.begin(), in.end());
        int steps = MOD;
        for(int v = 0; v <= mn; v ++)
        {
            int ans = 0;
            for(auto e : in)
                ans += dp[e - v];
            
            steps = std::min(steps, ans);
        }
        cout << steps << endl;
    }
    return 0;
}
View Code

Lesson learnt: a lot complex scenarios can be transformed into simpler ones..

HackerRank - "Equal"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4625859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!