HackerRank - "Equal"

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;
}

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

时间: 2024-08-28 11:19:39

HackerRank - "Equal"的相关文章

HackerRank &quot;Equal Stacks&quot;

Greedy - though simple, but fun! #include <vector> #include <iostream> using namespace std; int main(){ int n1; int n2; int n3; cin >> n1 >> n2 >> n3; long long l1 = 0, l2 = 0, l3 = 0; vector<int> h1(n1); for(int h1_i =

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

Hackerrank - The Grid Search

https://www.hackerrank.com/challenges/the-grid-search/forum 今天碰见这题,看见难度是Moderate,觉得应该能半小时内搞定. 读完题目发现是纯粹的一道子矩阵匹配问题,想想自己以前没做过,肯定能学到新算法,于是就开搞了. 于是上网搜到了Rabin-Karp算法,一种基于hashing的模式匹配算法.尽管连一维的我也没写过,但看了思想以后觉得推广到二维应该也不会很难. 于是有了以下代码,原理就是计算子矩阵的hash key.以hash k

Hackrank Equal DP

Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and may have distributed the chocolates unequally. One of the program managers gets to know this and orders Christy

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum

【HackerRank】 Filling Jars

Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operations. Each operation is described by 3 integers a, b and k. Here, a and b are index of the jars, and k is the number of candies to be added inside each

【HackerRank】Missing Numbers

Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers from List A got left out. Can you find out the

[LeetCode] Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user

1.错误描写叙述 553 Mail from must equal authorized user com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333) at com.sun.mail.smtp.SMTPTransport.mail