DLUTOJ 1331 Maximum Sum
Time Limit: 1 Sec Memory Limit: 128 MB
Description
Input
Output
For every test case output the maximum value asked above in a newline.
Sample Input
Sample Output
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的更多相关文章
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- Find the Maximum sum
Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...
随机推荐
- tp登录方法
先根据输入的名字检测是否存在数据表中,如果有继续进一步验证密码,除了select(),find()可以查询数据外,还可以用getByXxx()指定数据表字段查询, 比如数据表有username字段,就 ...
- Sublime Text 3 文本编辑器
1.安装下载 下载地址:http://www.cr173.com/soft/121149.html http://www.xiazaiba.com/html/24343.html 官网 http:// ...
- title与h1的区别
title与h1的联系: 从网站角度看,title更重于网站信息.title可以直接告诉搜索引擎和用户这个网站是关于什么主题和内容的. 从文章角度看,h1则是用于概括文章主题.用户进入内容页,想看到的 ...
- usb驱动开发7之接口描述符
前面struct usb_interface里表示接口设置的struct usb_host_interface被有意的飘过了,咱们在这节主要讲讲这个结构体,同样在include/linux/usb.h ...
- 异步fifo的设计
本文首先对异步 FIFO 设计的重点难点进行分析 最后给出详细代码 一.FIFO简单讲解 FIFO的本质是RAM, 先进先出 重要参数:fifo深度(简单来说就是需要存多少个数据) ...
- How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7
How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7 The problem Google developers s ...
- 小程序基础09:视图层之WXML
1.WXML WXML是框架设计的一套标签语言,结合基础组件,事件系统,可以构建出页面的结构. 用以下的例子来看看WXML有什么能力: 1.1数据绑定 <view>{{age}}</ ...
- [MySQL] 按日期进行统计(前一天、本周、某一天)
在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time统计当天 sql语句为: select * from user where date(log_time ...
- 《图解tcp/ip》读书笔记(二)
<图解tcp/ip>读书笔记(二) 本周主要阅读的是本书的第三章--数据链路. 当然了,从某些角度讲,我认为这一章就是计算机网络的最基本的内容之一.整章讲述了数据链路层的作用和相关技术,主 ...
- Windows的Subversion备份脚本
2015-12-08更新:备份时添加--revision head 只备份最新的版本,已从脚本中移除. 2013-12-09更新:forfiles命令添加错误输出日志. 2013-12-04更新:添加 ...