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. iptables常用命令之配置生产环境iptables及优化

    第一步:清空当前的所有规则和计数 iptables -F #清空所有的防火墙规则 iptables -X #删除用户自定义的空链 iptables -Z #清空计数 第二步:配置允许ssh端口连接 i ...

  2. 什么是NoSQL

    NoSQL = Not Only SQL 不仅仅是SQL NoSQL,指的是非关系型的数据库(没有声明性查询语言,没有预定义的模式,可以为键 - 值对存储,列存储,文档存储,图形数据库).不同于传统的 ...

  3. VueJS 事件修饰符

    事件修饰符 在事件处理程序中调用 event.preventDefault() 或 event.stopPropagation()是非常常见的需求.尽管我们可以在 methods 中轻松实现这点,但更 ...

  4. cas错误:org.jasig.cas.client.validation.TicketValidationException: No principal was found in the response from the CAS server.

    这个问题困扰了我好几天,最终被这个哥们解决了,具体请参考:http://www.oschina.net/question/252484_149958?sort=time

  5. oracle 创建用户并赋权 清空用户表

    create user BUSM identified by BUSM; grant connect,resource,dba to BUSM; grant select any table to B ...

  6. JS基础——循环很重要

    介绍循环之前,首先要说一下同样很重要的if-else结构,switch-case结构 ①if-else结构 if(判断条件) { 条件为true时执行 } else{ 条件为false时执行 } ②i ...

  7. 阿里云CentOS-7.2安装mysql

    我下载的阿里云的服务器系统centos7.2是纯内核版本,并没有其他的工具,所以这个系统是非常干净的.所以我就需要给系统安装一一些工具,来方便系统的管理与操作,我们上面讲到了关于服务器的yum的配置在 ...

  8. express4.x的使用

    ①.安装 npm install -g  express   ②.创建应用 express [目录] 会在目录下生成 node_modules, 存放所有的项目依赖库.(每个项目管理自己的依赖,与Ma ...

  9. 关于echarts的那些事(地图标点,折线图,饼图)

    前记:离上一篇博客的发布已经过去两个月了,这期间总想写点什么,却怎么都写不出来,一直拖到了现在.现在的感觉,不是像这期间一样,想好好整理一番,写一篇好博客,却写不出来.事实发现,随心就好,较好的博客, ...

  10. iOS 制作自动打包脚本 Xcode8.3.2

    本文包含以下内容: 前言 1.shell脚本的编写 2.xcodebuild命令 3.完整的可用示例 参考资料 前言 做iOS开发,打包APP是比较频繁的事情,每次都手动去配置一堆东西确实是比较乏味. ...