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但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...
随机推荐
- CodeForces 582A【multiset使用样例】
题意: 给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数. 首先对数字进行排序.复杂度n*log(n^2). 这题的证明有官方的英文题解==在这直接贴英文题解 ...
- codeforces 873E(枚举+rmq)
题意 有n(n<=3000)个人参与acm比赛,每个人都有一个解题数,现在要决定拿金牌的人数cnt1,拿银牌的人数cnt2,拿铜牌的人数cnt3,各自对应一个解题数区间[d1,c1],[d2,c ...
- Fragment实践之聊天窗体
前几天刚学了android的fragment,总是停留在简单的demo,也许永远都学不会. 今天,我要动手向我的聊天软件开刀.今天.用Fragment来实现一个例如以下图效果的聊天界面. waterm ...
- 软件质量之道:PCLint之中的一个
故天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能. 孟子 1引子 今天听老韩一席话,当真是感慨万千啊.心怀斗志昂扬.奋斗十年,到头来.却看到身 ...
- Java经常使用日期操作具体解释
Date类型大多数时间分量计算方法已经被Calendar代替 Date经常用法setTime getTime() new Date();默认获取当前的时间 SimpleDateFormat用来格式化和 ...
- 返回零长度的数组或者集合,而不是null
<<Effective Java>> 第四十三条:返回零长度的数组或者集合,而不是null 假设一个方法的返回值类型是集合或者数组 .假设在方法内部须要返回的集合或者数组是零长 ...
- 两个喜欢的"新"C#语法
现在C#比较新的语法,我都十分喜欢. 比如属性可设默认值: public string Name { get; set; } = "张三"; 还有一个就是拼接字符串. 以往,通常都 ...
- jquery获取兄弟元素
按照w3c school的指引,jquery中,要获得一个元素的兄弟,可以用 prev().next()两种方法.顾名思义,prev()获得前一个,next()获得后面一个. 问题是,如果存在前后兄弟 ...
- 那条linq语句为啥这么慢
目前所在的项目大量使用了linq,结果有个地方出现了严重的性能问题.一个统计需要3.40秒.头头焦头烂额之际,也让我看看. 我向来喜欢性能调优,自诩编码极为注重性能.曾几何时,也动不动就把性能挂在嘴边 ...
- oracle 10g 实例用localhost无法访问的处理
我在笔记本上安装了一个Oracle10g,安装好了之后,查看E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora文件,发现SID对应的IP地 ...