HackerRank - Sherlock and The Beast

Greedy beats DP this time...

I tried several DP solutions first, but all failed with RE\TLE. If you ‘feel‘ the problem, Greedy should be working:

(A solution from discussion)

def getPivot(n):
    while n > 0:
        if n % 3 == 0:
            break;
        else:
            n -= 5
    return n

T = input()
for i in xrange(T):
    N = int(input())
    pivot = getPivot(N)
    if pivot < 0:
        print -1
    else:
        str = ‘‘
        repeat = pivot / 3
        while repeat > 0:
            str += ‘555‘
            repeat -= 1
        repeat = (N - pivot) / 5
        while repeat > 0:
            str += ‘33333‘
            repeat -= 1
        print str
时间: 2024-10-27 17:34:54

HackerRank - Sherlock and The Beast的相关文章

【HackerRank】 Sherlock and The Beast

Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has b

Sherlock and The Beast

Problem Statement Sherlock Holmes is getting paranoid about Professor Moriarty, his arch-enemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has been

HackerRank - Sherlock and Queries

All about pruning and duplication removal. Took me several submissions to get it AC: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <unordered_map> using namespace

HackerRank - &quot;Sherlock and GCD&quot;

This is a very smart observation:http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # from functools import reduce def gcd(a, b): if a == 1 or b == 1: return 1 if a % b

HackerRank - Sherlock and Anagram

Please note input constraints. String length will not exceed 100, which means, we can use relatively naive representation\calculation for anagrams: sorting. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【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

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

hackerrank maxsum mod

https://www.hackerrank.com/challenges/maximise-sum/submissions/code/12028158 hackerrank 子数组和模m的最大值 时间复杂度nlgn, 主要是证明一点,presum[i]-presum[j] 对于0<j<i的j,最大的是第一个大于presum[i]的presum[j] 证明如下 Quro的讨论,感觉还是证明的过程不够细致 http://www.quora.com/What-is-the-logic-used-i