codeforces 880E. 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 indicesb1, 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}.
/*
折半搜索,把取模后的和存起来
双指针统计答案
*/
#include<bits/stdc++.h> #define N 300000 using namespace std;
int a[N],p[N],q[N];
int k,t,ans,n,m,b,dep,flag; inline int max(int x,int y){return x>y? x:y;} inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void dfs(int now,int sum)
{
if(now==dep)
{
if(!flag)
{
p[++k]=sum,p[++k]=(sum+a[b])%m;return;
}
else
{
q[++t]=sum,q[++t]=(sum+a[n])%m;
return ;
}
}
dfs(now+,sum);
dfs(now+,(sum+a[now])%m);
} int main()
{
n=read(),m=read(),b=n>>;dep=b;
for(int i=; i<=n; ++i) a[i]=read();
if(n==) printf("%d",a[]%m),exit();
flag=;dfs(,);
dep=n;flag=;dfs(b+,);
int L=,R=t;
sort(p+,p+k+);sort(q+,q+t+);
while(L<=k)
{
while(p[L]+q[R]>=m) --R;
ans=max(ans,p[L]+q[R]),++L;
}
ans=max(ans,p[k]+q[t]-m);
printf("%d",ans);
return ;
}
codeforces 880E. Maximum Subsequence(折半搜索+双指针)的更多相关文章
- CF 888E Maximum Subsequence——折半搜索
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
- 【CF888E】Maximum Subsequence 折半搜索
[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)
[Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...
- codeforces912E(折半搜索+双指针+二分答案)
E. Prime Gift E. Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 888E Maximum Subsequence
原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Educational Codeforces Round 32 E. Maximum Subsequence
题目链接 题意:给你两个数n,m,和一个大小为n的数组. 让你在数组找一些数使得这些数的和模m最大. 解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...
随机推荐
- 一份关于webpack2和模块打包的新手指南(一)
webpack已成为现代Web开发中最重要的工具之一.它是一个用于JavaScript的模块打包工具,但是它也可以转换所有的前端资源,例如HTML和CSS,甚至是图片.它可以让你更好地控制应用程序所产 ...
- RabbitMQ最佳实践
在使用消息机制时,我们通常需要考虑以下几个问题: 消息不能丢失 保证消息一定能投递到目的地 保证业务处理和消息发送/消费的一致性 本文以RabbitMQ为例,讨论如何解决以上问题. 消息持久化 如果希 ...
- 寒武纪camp Day2
补题进度:8/10 A(计数+BIT) 题意: 给一个长度为n的数组a[],任意选0<=i<=j<n,将a[i]~a[j]从小到大排序,形成新的数组.问有多少个不同的新数组. N,a ...
- Java面试题,深入理解final关键字
final关键字 final的简介 final可以修饰变量,方法和类,用于表示所修饰的内容一旦赋值之后就不会再被改变,比如String类就是一个final类型的类. final的具体使用场景 fina ...
- 配置Python 2.7.1外加环境pywin32-216.win32-py2.7
python-2.7.1 安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...
- webpack-Modules(模块)
模块(Modules) 在模块化编程中,开发者将程序分解成离散功能块(discrete chunks of functionality),并称之为模块. 每个模块具有比完整程序更小的接触面,使得校验. ...
- Lync 2013 与Exchange 2013 UM&UC 集成!
设置好对应的拨号计划.我们设置分机号码为4位: 配置好接入号码为5000: 配置自己主动助理号码为6000: 改动UM拨号模式为双模式: Set-UMService -identity Exch ...
- Linux下进程信息的深入分析
这里我们主要介绍进程的状态,进程的状态可以通过/proc/PID/status来查看,也可以通过/proc/PID/stat来查看. 如果说到工具大家用的最多的ps也可以看到进程的信息.这里我们通过/ ...
- Codeforces Round #221 (Div. 2) D
有点郁闷的题目,给了2000ms,可是n,m的范围已经是5000了.5000 * 5000一般在别的OJ已经是超了2000ms,一開始不敢敲.看了下别人有n*m的潜逃循环,原来CF的机子如此的强大,一 ...
- sharpdevelop 调整代码字体显示大小
SharpDevelop中使用ctrl+鼠标滚轮可以调整代码的字体显示大小