Codeforces 888E Maximum Subsequence
1 second
256 megabytes
standard input
standard output
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, ..., bk (1 ≤ b1 < b2 < ... < bk ≤ n) in such a way that the value of 
 is maximized. Chosen sequence can be empty.
Print the maximum possible value of 
.
The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print the maximum possible value of 
.
4 4
5 2 4 1
3
3 20
199 41 299
19
In the first example you can choose a sequence b = {1, 2}, so the sum 
 is equal to 7 (and that's 3 after taking it modulo 4).
In the second example you can choose a sequence b = {3}.
分析:看到n≤35的条件时确实应该想到什么——假如n≤25,那么直接穷举2n情况即可,那么这个想法能否应用到35的情况中呢?答案时肯定的:使用Meet-in-the-Middle算法,即将原数组拆成两半,每一半都枚举2n/2个和(当然要mod m了),然后分别排序,枚举左半边取的和,再在右半边二分查找能拼出来的最大的和(假如左半边为x,则对右半边数组lower_bound(c,c+(1<<nr),m-x)-c-1,最后减1的原因是恰好求出小于m-x的最大值,即为答案)。这个算法能够成立的原因是:左右两边是加的关系,因此不存在加起来大于m,并且mod m后比不大于m的情况大——即(x+y)mod m<=max(x,y) mod m (0≤x,y<m)。
实现起来还是很简单的。
Codeforces 888E Maximum Subsequence的更多相关文章
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
		
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
 - codeforces 880E. Maximum Subsequence(折半搜索+双指针)
		
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
 - CF 888E Maximum Subsequence——折半搜索
		
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
 - CF 888E Maximum Subsequence
		
一道比较套路的题,看到数据范围就差不多有想法了吧. 题目大意:给一个数列和\(m\),在数列任选若干个数,使得他们的和对\(m\)取模后最大 取膜最大,好像不能DP/贪心/玄学乱搞啊.\(n\le35 ...
 - 888E - Maximum Subsequence  中途相遇法
		
Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...
 - [CodeForces - 197C] C - Lexicographically Maximum Subsequence
		
C - Lexicographically Maximum Subsequence You've got string s, consisting of only lowercase English ...
 - Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
		
题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...
 - Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value  贪心
		
题意:E.Maximum Subsequence Value 题意: 给你n 个元素,你挑选k个元素,那么这个 k 集合的值为 ∑2i,其中,若集合内至少有 max(1,k−2)个数二进制下第 i 位 ...
 - 1007. Maximum Subsequence Sum (25)
		
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
 
随机推荐
- 【深度学习系列】用PaddlePaddle和Tensorflow实现GoogLeNet InceptionV2/V3/V4
			
上一篇文章我们引出了GoogLeNet InceptionV1的网络结构,这篇文章中我们会详细讲到Inception V2/V3/V4的发展历程以及它们的网络结构和亮点. GoogLeNet Ince ...
 - 小白的Python之路 day2   列表、元组操作
			
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 names = ['Tom','Jack','Qian'] 通过下标访问列表中 ...
 - if;脚本中退出语句:exit 数字,用$?查时为exit设置的数字,此数字为程序执行完后的返回数据,可以通过此方法自动设定
			
if [ 条件 ];then 代码 fi if [ 条件 ] then 代码 fi [root@localhost ~]# df 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda5 % ...
 - iOS masonry九宫格 单行 多行布局
			
Masonry是个好东西,在当前尺寸各异的iOS开发适配中发挥着至关重要的作用,由于项目中Masonry布局用的比较多,对于UI布局也有了一些自己的理解,经常会有人问道Masonry布局九宫格要怎么布 ...
 - 关于《ASP.NET MVC企业级实战》
			
大家好,我的书<ASP.NET MVC企业级实战>已经出版啦,感谢大家过去的关注与支持!前言部分,出版的时候漏了部分内容,我这里将其贴出来. 本书提供源码和教学PPT课件!(源码在书中第3 ...
 - 三菱Q系列PLC的智能功能模块程序
			
一.模拟量输入模块Q64AD 1.模块开关或者参数设置 1.1I/O分配 1.2开关设置使用通道1,0-5v, 1.3使用GX configurator设置自动刷新PLC设置智能功能模块参数,即将模拟 ...
 - ArcGIS API for JavaScript 4.2学习笔记[14] 弹窗的位置、为弹窗添加元素
			
这一节我们来看看弹窗的位置和弹窗上能放什么. 先一句话总结: 位置:可以随便(点击时出现或者一直固定在某个位置),也可以指定位置 能放什么:四种,文字.媒体(图片等).表格.附件. [Part I 位 ...
 - sublime学习笔记
			
学习课程地址:快乐的sublime编辑器_sublime编辑器使用 另可参考笔记地址:http://c.haoduoshipin.com/happysublime/ PS:博主的一些文章地址:http ...
 - Nginx 错误处理方法: bind() to 0.0.0.0:80 failed
			
Nginx 错误处理方法: bind() to 0.0.0.0:80 failed 今天启动window上的nginx总是报错 错误信息是bind() to 0.0.0.0:80 failed (10 ...
 - 优化css选择器
			
1.css选择器效率排行从高到低如下: id选择器(#head) 类选择器(.content) 标签选择器(p,h1) 相邻选择器(h1+p) 子选择器(ul < li)