CF 888E Maximum Subsequence

一道比较套路的题,看到数据范围就差不多有想法了吧。

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

取膜最大,好像不能DP/贪心/玄学乱搞啊。\(n\le35\)?果断meet in middle

考虑我们已经搜出了序列前一半的解,那么怎么根据后面的结果合并出结果?

设我们现在得到的和为\(x\)(对\(m\)取膜后),我们令一个数\(y=m-x\),然后在前面的解中查找\(y\)的前驱即可

接下来进行简单的证明:

  • 若可以找到前驱\(z\),由于\(z<y\),故\(x+z<m\)。又因为\(z=max(s\in[1,y-1])\),故此时值最大。
  • 若无法找到前驱\(z\),此时我们取任何一个值\(s\)都会导致\(x+s>x+y=m\),此时\((x+s)\ mod\ m<x\)(这个很好理解吧)

于是我们每次都二分找出前缀,并取\(max\)即可。

CODE

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int N=40;
int a[N],n,m,sum[1<<20],cnt,ans;
inline char tc(void)
{
    static char fl[100000],*A=fl,*B=fl;
    return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
    x=0; char ch; while (!isdigit(ch=tc()));
    while (x=(x<<3)+(x<<1)+ch-‘0‘,isdigit(ch=tc()));
}
inline int find(int x)
{
    int l=1,r=cnt,res;
    while (l<=r)
    {
        int mid=l+r>>1;
        if (sum[mid]<x) res=sum[mid],l=mid+1; else r=mid-1;
    }
    return res;
}
inline void init(int now,int tot)
{
    if (now>(n>>1)) { sum[++cnt]=tot; return; }
    init(now+1,(tot+a[now])%m); init(now+1,tot);
}
inline void DFS(int now,int tot)
{
    if (now>n) { ans=max(ans,tot+find(m-tot)); return; }
    DFS(now+1,(tot+a[now])%m); DFS(now+1,tot);
}
int main()
{
    //freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
    register int i; read(n); read(m);
    for (i=1;i<=n;++i) read(a[i]);
    init(1,0); sort(sum+1,sum+cnt+1); DFS((n>>1)+1,0);
    return printf("%d",ans),0;
}

原文地址:https://www.cnblogs.com/cjjsb/p/9426866.html

时间: 2024-10-04 20:13:45

CF 888E Maximum Subsequence的相关文章

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

1007. 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 最大子序列和的进击之路

本文解决最大子序列和问题,有两个题目组成,第二个题目比第一个要求多一些(其实就是要求输出子序列首尾元素). 01-复杂度1 最大子列和问题   (20分) 给定KK个整数组成的序列{ N1??, N2??, ..., NK?? },"连续子列"被定义为{ N?i??, Ni+1 ..., Nj },其中 1≤i≤j≤K."最大子列和"则被定义为所有连续子列元素的和中最大者.例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4,

pat1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

Algorithm for Maximum Subsequence Sum z

MSS(Array[],N)//Where N is the number of elements in array { sum=0; //current sum max-sum=0;//Maximum Sum seq-start=0;//start of the subsequence seq-end=0;//end of the subsequence for(i=0;i<N;i++){ sum=sum+Array[i]; if(sum<0){ sum=0; seq-start++; }