codeforces 880E. Maximum Subsequence(折半搜索+双指针)

E. Maximum Subsequence

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 .

Input

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).

Output

Print the maximum possible value of .

Examples

input

Copy

4 45 2 4 1

output

Copy

3

input

Copy

3 20199 41 299

output

Copy

19

Note

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=0,f=1;char c=getchar();
    while(c>‘9‘||c<‘0‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;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+1,sum);
    dfs(now+1,(sum+a[now])%m);
}

int main()
{
    n=read(),m=read(),b=n>>1;dep=b;
    for(int i=1; i<=n; ++i) a[i]=read();
    if(n==1) printf("%d",a[1]%m),exit(0);
    flag=0;dfs(1,0);
    dep=n;flag=1;dfs(b+1,0);
    int L=0,R=t;
    sort(p+1,p+k+1);sort(q+1,q+t+1);
    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 0;
}

原文地址:https://www.cnblogs.com/L-Memory/p/9898815.html

时间: 2024-10-28 23:29:21

codeforces 880E. Maximum Subsequence(折半搜索+双指针)的相关文章

【CF888E】Maximum Subsequence 折半搜索

[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一下子知道怎么做了,吓得我赶紧把tag屏蔽了. 我们将原序列拆成两半,每一部分都暴力搜索出所有的子序列之和,用set存起来.然后枚举前一半的所有子序列和,设其为x,则使得总和%m最大的右半部分子序列和一定是所有<m-x的数中最大的那个,在set里找一下前驱就行了. #include <cstdio&

Educational Codeforces Round 32 E. Maximum Subsequence

E. Maximum Subsequence 题意: n 个数,选出其中  k 个数,使得他们的和对 m 取模后最大. 输出这个最大值. tags:注意到 n 很小, 所以折半枚举. // E #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; +

CF888E Maximum Subsequence

CF888E Maximum Subsequence 有一种叫做折半搜索的好东西 我们把数列劈成两半,分别搜索,再合并 合并可以排序+二分或者排序+单调性 代码极短 #include<bits/stdc++.h> using namespace std; const int N=37; const int M=5000005; typedef long long ll; int n; ll m; ll a[N]; int mi; ll s1[M],s2[M]; int cnt1=0,cnt2=

Maximum Subsequence Sum - 最大子列和问题_C语言实现

第一次写这方面的blog.自己也是初次接触相关知识,写的有不妥的地方十分欢迎大家指正~ 这是浙大PAT上的一道算法题(据说是浙大04年研究生复试题),题目是这样的: Maximum Subsequence Sum Given a sequence of KK integers { N_1N?1??, N_2N?2??, ..., N_KN?K?? }. A continuous subsequence is defined to be { N_iN?i??, N_{i+1}N?i+1??, ..

PAT 1007——Maximum Subsequence Sum

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

01-复杂度2. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

Maximum Subsequence Sum(接上篇)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

Codeforces 484B Maximum Value(高效+二分)

题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,并且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然后枚举k,每次用二分找到小于k?aj并且最大的ai,维护答案,过程中加了一些剪枝. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn =