传送门

Time Limit: 1 Sec  Memory Limit: 128 MB 


Description

You are given an array of size N and another integer M.Your target is to find the maximum value of sum of subarray modulo M.
Subarray is a continous subset of array elements.
Note that we need to find the maximum value of (Sum of Subarray)%M , where there are N*(N+1)/2 possible subarrays.

Input

First line contains T , number of test cases to follow. Each test case consits of exactly 2 lines. First line of each test case contain 2 space separated integers N and M, size of the array and modulo value M. 
Second line contains N space separated integers representing the elements of the array.
2 ≤ N ≤ 10^5 
1 ≤ M ≤ 10^14 
1 ≤ elements of the array ≤ 10^18 
2 ≤ Sum of N over all test cases ≤ 500000

Output

For every test case output the maximum value asked above in a newline.

Sample Input

1 5 7 3 3 9 9 5

Sample Output

6

HINT

Max Possible Sum taking Modulo 7 is 6 , and we can get 6 by adding first and second element of the array

Source


Solution:

$先预处理出数组在模M下的前缀和sum[ ].$

$枚举区间起点L,然后在sum[L+1, ..., n]上查找两个值:$

\[v_1=max\{sum[i]: sum[i] < sum[L]\}\]

\[v_2=max\{sum[i]: sum[i] \ge sum[L]\}\]

然后用

\[max(v_{1}-a[L]+M, v_{2}-a[L])\]

更新答案


 Implementation:可以用map,也可以用multiset.

map版

忘了map自带lower_bound函数,而且只能用这个lower_bound,不能写成lower_bound(b, e, k)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N(1e5+);
map<LL,int> mp;
int n;
LL m, a[N]; map<LL,int>::iterator it;
int main(){
int T;
for(scanf("%d", &T); T--; ){
scanf("%d%lld", &n, &m);
mp.clear();
for(int i=; i<=n; i++) scanf("%lld", a+i), a[i]+=a[i-], a[i]%=m, mp[a[i]]++;
mp[]++;
LL ans=;
for(int i=; i<n; i++){
mp[a[i]]--;
if(!mp[a[i]]) mp.erase(a[i]);
ans=max(ans, ((--mp.end())->first-a[i]+m)%m);
it=mp.lower_bound(a[i]);
if(it!=mp.begin())
ans=max(ans, (--it)->first-a[i]+m);
}
printf("%lld\n", ans);
}
return ;
}

multiset 版

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N(1e5+);
multiset<LL> ms;
int n;
LL m, a[N]; multiset<LL>::iterator it; int main(){
int T;
for(scanf("%d", &T); T--; ){
scanf("%d%lld", &n, &m);
ms.clear();
for(int i=; i<=n; i++) scanf("%lld", a+i), a[i]+=a[i-], a[i]%=m, ms.insert(a[i]);
ms.insert();
LL ans=;
for(int i=; i<n; i++){
it=ms.find(a[i]);
ms.erase(it);
ans=max(ans, (*--ms.end()-a[i]+m)%m);
it=ms.lower_bound(a[i]);
if(it!=ms.begin())
ans=max(ans, *--it-a[i]+m);
}
printf("%lld\n", ans);
}
return ;
}

写这道题主要是复习C++ STL。上面提到的查询也可以用划分树来写,不过麻烦了许多。BST又不会敲,sigh。。。。。

DLUTOJ 1331 Maximum Sum的更多相关文章

  1. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  2. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  3. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  5. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  6. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  7. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  8. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  9. Find the Maximum sum

    Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...

随机推荐

  1. iOS中使用RSA对数据进行加密解密

    RSA算法是一种非对称加密算法,常被用于加密数据传输.如果配合上数字摘要算法, 也可以用于文件签名. 本文将讨论如何在iOS中使用RSA传输加密数据. 本文环境 mac os openssl-1.0. ...

  2. 关联:objc_getAssociatedObject和objc_setAssociatedObject使用

    为UIButton的category添加属性 UIButton+subTitle.h #import <UIKit/UIKit.h> #import <objc/runtime.h& ...

  3. poj3371

    Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2269 Accepted: 710 De ...

  4. ruby on rails gem install pg时无法安装

    gem install pg -v '0.18.2' Building native extensions. This could take a while... ERROR: Error insta ...

  5. Android 中调试手段 打印函数调用栈信息

    下面来简单介绍下 android 中的一种调试方法. 在 android 的 app 开发与调试中,经常需要用到打 Log 的方式来查看函数调用点. 这里介绍一种方法来打印当前栈中的函数调用关系 St ...

  6. C# 类型运算符重载在类继承中的调用测试

    这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest ...

  7. 如何配置CentOS或者RedHat5.X、6.X、7.X的网络yum源

    第一步:找到一个可靠的yum源 中科大帮助:https://lug.ustc.edu.cn/wiki/mirrors/help/centos源:http://mirrors.ustc.edu.cn/c ...

  8. Asp.net设计模式笔记之一:理解设计模式

    GOF设计模式著作中的23种设计模式可以分成三组:创建型(Creational),结构型(Structural),行为型(Behavioral).下面来做详细的剖析. 创建型 创建型模式处理对象构造和 ...

  9. chrome设置--disable-web-security解决跨域

    我们可以通过使用chrome命令行启动参数来改变chrome浏览器的设置,具体的启动参数说明参考这篇介绍.https://code.google.com/p/xiaody/wiki/ChromiumC ...

  10. MATLAB中提高fwrite和fprintf函数的I/O性能

    提高fwrite和fprintf函数的I/O性能 http://www.matlabsky.com/thread-34861-1-1.html     今天我们将讨论下著名的fwrite(fprint ...