Maximum Subarray Sum

题意

给你一个大小为N的数组和另外一个整数M。你的目标是找到每个子数组的和对M取余数的最大值。子数组是指原数组的任意连续元素的子集。

分析

参考

求出前缀和,问题变成了O(n*n)复杂度的问题,但是仍然不能解决问题。

设prefix为前缀和,设i < j,一般都是通过算sum = prefix[j] - prefix[i]求和的最大值,但是本题中有取模,要求sum最大。

第一种情况:如果prefix[j] > prefix[i],sum < prefix[j],这种情况就不需要考虑了。

第二种情况:prefix[j] < prefix[i],又i < j,那么prefix[j]是取模后得到的值,此时sum = prefix[j] + m - prefix[i],要sum尽可能的大,则要求prefix[i]尽可能的小,而prefix[i] > prefix[j],所以每次要

前面出现过的前缀和里找比它大一点的值,这里就想到了C++里的set,有序的集合,set.upper_bound二分查找,并插入前缀和,复杂度O(nlogn)。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
ll a[MAXN];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T, n;
ll m;
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i = 0; i < n; i++)
{
ll x;
cin >> x;
if(i == 0) a[0] = x % m;
else a[i] = (a[i - 1] % m + x) % m;
}
set<ll> st;
ll mx = 0;
for(int i = 0; i < n; i++)
{
set<ll>::iterator x = st.upper_bound(a[i]);
if(x != st.end()) mx = max(mx, a[i] + m - *x);
mx = max(mx, a[i]);
st.insert(a[i]);
}
cout << mx << endl;
}
return 0;
}

Maximum Subarray Sum的更多相关文章

  1. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  2. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

  3. leetcode1186 Maximum Subarray Sum with One Deletion

    思路: 最大子段和的变体,前后两个方向分别扫一遍即可. 实现: class Solution { public: int maximumSum(vector<int>& arr) ...

  4. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)

    描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  6. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  7. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  9. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

随机推荐

  1. 用Rvm安装Ruby,Rails运行环境及常见错误解决方法

    一.安装Rvm 1.下载安装Rvm $ curl -L https://get.rvm.io | bash -s stable 此时可能出现错误:"gpg: 无法检查签名:找不到公钥&quo ...

  2. 百度UEditor图片上传或文件上传路径自定义

    最近在项目中使用到百度UEditor的图片以及文件上传功能,但在上传的时候路径总是按照预设规则来自动生成,不方便一些特殊文件的维护.于是开始查看文档和源代码,其实操作还是比较简单的,具体如下: 1.百 ...

  3. spring学习总结一----控制反转与依赖注入

    spring作为java EE中使用最为广泛的框架,它的设计体现了很多设计模式中经典的原则和思想,所以,该框架的各种实现方法非常值得我们去研究,下面先对spring中最为重要的思想之一----控制反转 ...

  4. C语言学习第三章

    写在课前,提醒自己写代码的时候一定要注意不能漏写符号!提醒自己写代码的时候一定要注意不能漏写符号!提醒自己写代码的时候一定要注意不能漏写符号! 今天主要学习掌握if...else条件结构,多重if条件 ...

  5. 最简单bat教程

    请移到此处查看 http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.html

  6. 0基础搭建Hadoop大数据处理-初识

    在互联网的世界中数据都是以TB.PB的数量级来增加的,特别是像BAT光每天的日志文件一个盘都不够,更何况是还要基于这些数据进行分析挖掘,更甚者还要实时进行数据分析,学习,如双十一淘宝的交易量的实时展示 ...

  7. glassfish PWC6351: In TLD scanning 系统找不到指定的文件问题解决

    [2017-04-25T21:26:09.391+0800] [glassfish 4.1] [WARNING] [] [org.apache.jasper.runtime.TldScanner] [ ...

  8. Spring Boot 整合 MyBatis

    前言 现在业界比较流行的数据操作层框架 MyBatis,下面就讲解下 Springboot 如何整合 MyBatis,这里使用的是xml配置SQL而不是用注解.主要是 SQL 和业务代码应该隔离,方便 ...

  9. SQL SERVER大话存储结构(2)_非聚集索引如何查找到行记录

              如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持!      1 行记录如何存储     这里引入两个 ...

  10. SharePoint Application Page启用匿名访问

    现在的项目需要使用sharepoint application page来展示图片影像,并让其它应用系统匿名访问,经过一番认真研究,主要有下面的步骤: 1. 在web applicaiton leve ...