HackerRank - "Maximise Sum"

It is modulo version of "max sum of contiguous subarray". So similar pattern can be applied too, but with tricks regarding to modulo ops.

I checked editorials, and found that, the quick\correct solution requires sharp insight into internal characteristics and mechanism of one problem... which I‘m still lack with...

Modulo, you should feel of it is as a roll-over game. If within the scope, like (3 - 1)%3, there‘s nothing special; and if beyond the scope like (1 - 3)%3, you get it roll-over from the beginning..

And another key trick is, std::lower_bound() upon std::set is slower than set::lower_bound()..

#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <string>
#include <climits>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;

int dp(vector<int> &in, int m)
{
    int ret = 0;
    size_t len = in.size();

    vector<int> dp(len, 0);

    for (int ilen = 1; ilen <= len; ilen ++)
    for (int i = 0; i <= len - ilen; i++)
    {
        dp[i] += in[i + ilen - 1];
        ret = std::max(ret, dp[i] % m);
    }
    return ret;
}

int main()
{
    int t; cin >> t;
    while (t--)
    {
        long long n, m; cin >> n >> m;

        long long x, prefix = 0, ret = 0;
        set<long long> s;
        s.insert(0);
        for (int i = 0; i < n; i++)
        {
            cin >> x;
            prefix = (prefix + x) % m;

            //    Case 1: prefix - 0
            //        (for smaller prefixes)
            ret = std::max(ret, prefix); 

            //    Case 2: prefix - smallest_larer + m
            //        (for larger prefixes)
            auto it = s.lower_bound(prefix + 1);
            if (it != s.end())
            {
                ret = std::max(ret, prefix - *it + m);
            }

            s.insert(prefix);
        }
        cout << ret << endl;
    }
    return 0;
}
时间: 2024-08-02 15:32:32

HackerRank - "Maximise Sum"的相关文章

日常小测:颜色 &amp;&amp; Hackerrank Unique_colors

题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献是独立的.我们可以在一次点分治中合在一块处理(为什么时间复杂度是对的呢,因为我们每次改动只会根据当前点的颜色进行变动,而不是所有颜色对着它都来一遍).每次先对重心单独计算答案贡献,此时也将当前区域的各个答案贡献计算出来,并以此为基础(之后称之为基准贡献,即代码中的tot).对于每一棵子树,我们先df

HackerRank - &quot;String Transmission&quot;

Classic and challenging DP! And you need combine several tricks together with DP to make it 100% pass. My main reference is here: https://github.com/havelessbemore/hackerrank/blob/master/algorithms/bit_manipulation/string-transmission.java Basically

51nod 1305 Pairwise Sum and Divide(数学分析题)

1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 取消关注 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) return sum

1305 Pairwise Sum and Divide(数学 ,规律)

HackerRank 1305 Pairwise Sum and Divide 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) return sum 给出数组A,由你来计算fun(A)的结果.例如:A = {1, 4, 1},fun(A) = [5/4] + [2

【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】Pairs

题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[i],那么只要看数组中是否存在ar[i]+K或者ar[i]-K就可以了,还是用HashMap在O(1)的时间完成这个操作. 题目有一点没说清楚的就是元素是否有重复,从Editorial来看似乎是没有重复,不过我还是用map的value记录了数出现的频率来处理了重复. 代码如下: 1 import j

【HackerRank】The Love-Letter Mystery

James找到了他的朋友Harry要给女朋友的情书.James很爱恶作剧,所以他决定要胡搞一下.他把信中的每个单字都变成了回文.对任何给定的字符串,他可以减少其中任何一个字符的值,例如'd'可以变成'c',这算是一次操作.(另外,他最多只能将字符的值减少至'a','a'不能再被减少成'z').找出将给定字符串转换成回文所需的最少操作次数. 输入格式 第一行包含整数 T 代表测试数据的组数. 接着 T 行各包含一个字符串. 输出格式 每个测试数据各输出一行,代表此数据需要的最少操作次数. 取值范围

【HackerRank】Ice Cream Parlor

Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items wh

HackerRank - &quot;Sam and sub&quot;

Brutal-force solution is not hard to think about. But linear space input usually indicates O(n) DP solution. State design: dp[i]: total sum until index i; and then you need some number manipulation to figure out the equation. https://www.hackerrank.c