CF888E Maximum Subsequence-折半搜索

题意:给一个数列和m,在数列任选若干个数,使得他们的和对m取模后最大。

注意到n<=35,直接枚举状态不行,考虑meeting in the middle。

那么的话我们直接暴力枚举两边的状态就好了,不过我们记录的是取模后的sum。。

现在主要解决合并答案的问题。都是套路是吧。。。

我们容易发现,如果我们枚举一边的答案,

另外一边有用的答案(有可能和当前枚举的构成最后答案的)仅有两种可能,

一种是和当前答案加起来<模数的,那么显然最大的那个最优,

另一种是>模数的,由于之前取了模,和不会超过二倍模数,那么显然也是最大的最优。

那么可以直接排序后一遍扫。我比较傻,直接二分查找的,多带了个log也过了。

#include <bits/stdc++.h>
using namespace std;
inline int gi() {
    int w=0,x=0; char ch=0;
    while (!(ch>=‘0‘&&ch<=‘9‘) ) {
        if (ch==‘-‘) w=1;
        ch=getchar ();
    }
    while (ch>=‘0‘&&ch<=‘9‘) {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar ();
    }
    return w?-x:x;
}

const int MAXN=262144;
int n,l,r,Mid,Ans,Mod,cntL,cntR,a[40],LefANS[MAXN],RigANS[MAXN];

void _DFS (int x,int sum) {
    if (x==(n>>1)+1) {
        LefANS[++cntL]=sum;
        return;
    }
    _DFS (x+1,sum);
    _DFS (x+1,(sum+a[x]%Mod)%Mod);
}

void _dfs (int x,int sum) {
    if (x==n+1) {
        RigANS[++cntR]=sum;
        return;
    }
    _dfs (x+1,sum);
    _dfs (x+1,(sum+a[x]%Mod)%Mod);
}

int search (int id) {
    l=1,r=cntR;
    while (l<r) {
        Mid=(l+r)>>1;
        if (LefANS[id]+RigANS[Mid]>=Mod) r=Mid;
        else l=Mid+1;
    }
    return r-1;
}

int main ()
{
    // BY BHLLX
    n=gi (), Mod=gi ();
    for (int i=1;i<=n;++i) a[i]=gi ();
    _DFS (1,0),_dfs ((n>>1)+1,0);
    sort (LefANS+1,LefANS+cntL+1);
    sort (RigANS+1,RigANS+cntR+1);
    for (int i=1;i<=cntL;++i)
        Ans=max (Ans,max (LefANS[i]+RigANS[search (i)],(LefANS[i]+RigANS[cntR])%Mod));
    printf ("%d\n", Ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Bhllx/p/9928202.html

时间: 2024-08-28 18:24:22

CF888E 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&

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

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=

CF888E Maximum Subsequence (折半枚举+ two-pointers)

题意 给定一个包含\(n\)个数的序列\(a\),在其中任选若干个数,使得他们的和对\(m\)取模后最大.(\(n\leq 35\)) 题解 显然,\(2^n\)的暴枚是不现实的...,于是我们想到了折半枚举,分成两部分暴枚,然后考虑合并,合并的时候用two-pointers思想扫一遍就行了. 其实这是一道折半枚举+Two-Pointers的很好的练手题 //最近CodeForces有点萎,可能会JudgementError,但已经评测过了,能AC,多交几次应该可以 #include <cstd

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; +

折半搜索

折半搜索 (meet in the middle) /* reference: translation: solution: trigger: note: * date: 2019.09.04 */ #include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,a,b) for(int i=a;i<=b;++i) #define dwn(i,a,b) for(int i=a;i>=b

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