Maximum Subarray Sum
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的更多相关文章
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- leetcode1186 Maximum Subarray Sum with One Deletion
思路: 最大子段和的变体,前后两个方向分别扫一遍即可. 实现: class Solution { public: int maximumSum(vector<int>& arr) ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- hadoop、Storm该选哪一个
如果hadoop.Storm还感觉混要,那么此篇文章将帮助你把他们完全区分 可以带着下面问题来阅读本文章: 1.hadoop.Storm各是什么运算 2.Storm为什么被称之为流式计算系统 3.ha ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- URL传中文参数导致乱码的解决方案之encodeURI
通过URL传中文参数时,在服务端后台获取到的值往往会出现乱码问题,解决方案有很多种,本文主要介绍如何通过encodeURI来解决中文乱码问题: first:前端传递参数的时候需要对中文参数进行两次en ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- centos下的activemq的配置及PHP的使用
一.安装JDK 1.下载JDK(官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...
- C语言精要总结-指针系列(一)
考虑到指针内容繁多,这里将指针作为一个系列,从简入繁,一点一点深挖并掌握这C语言的精华.初步计划如下 此文为指针系列第一篇: C语言精要总结-指针系列(一) 内存与地址 我们可以把内存看做一排连续的房 ...
- 微坑---微信小程序ios上时间字符串转换为时间戳时,在开发工具上和安卓手机上运行成功
给定一个时间字符串 var time="2017-02-27 16:42:53" js有三种转换为时间戳的方法:1.var timestamp = Date.parse(time ...
- iOS APP开发设置启动图片 Launch Image
一.添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或点下面的+号创建Launch Image: 这里首先说明一下尺寸: ...
- Play初识
2015年11月21日,写下这篇<Play初识> Play是神马呢?不是Google Play,而是一个java的web框架,因为它抛弃了传统的servlet模式的做法,国内网络连接pla ...
- 窝上课不听,how to learn C language easily(1)
C language 学习心得 附:为啥起这么霸气侧漏,招大神们鄙视的标题,正如我在<C language>随笔的介绍中写的,这是一个写个妹纸们看的C language的文章.没错!!写这 ...