首页 > 其他分享 >[CodeForce] Maximum Subsequence

[CodeForce] Maximum Subsequence

时间:2022-09-30 10:55:45浏览次数:76  
标签:compute int S2 Maximum Subsequence CodeForce ans out TreeSet

Problem Statement

 

1. N is up to 35, so trying all possible subsequences is too slow (2^35). We can apply the meet in the middle technique and divide A into two equal halves and compute all possible subsequences' sum modulo by M. This takes O(2^17) for each half of A, call the results as S1 and S2.

 

2. Then for each value V in S1, we have 2 options: 1. take V and does not take anything from S2; 2. take V and find the max W in S2 such that V + W <= M - 1. The answer is the max of both options for all V in S1.

3. To speed up the search in option 2, we can either sort S2 then perform binary search or store values of S2 in a sorted set.  

 

    static void solve(int testCnt)
    {
        for (int testNumber = 0; testNumber < testCnt; testNumber++) {
            int n = in.nextInt(), m = in.nextInt();
            int[] a = in.nextIntArrayPrimitive(n);
            if(n == 1) out.println(a[0] % m);
            else {
                int[] x1 = Arrays.copyOfRange(a, 0, n / 2);
                int[] x2 = Arrays.copyOfRange(a, n / 2, n);
                TreeSet<Integer> ts1 = compute(x1, m);
                TreeSet<Integer> ts2 = compute(x2, m);
                int ans = 0;
                for(int v : ts1) {
                    ans = Math.max(ans, v);
                    ans = Math.max(ans, v + ts2.floor(m - 1 - v));
                }
                out.println(ans);
            }
        }
        out.close();
    }
 
    static TreeSet<Integer> compute(int[] x, int m) {
        TreeSet<Integer> ts = new TreeSet<>();
        for(int i = 0; i < (1 << x.length); i++) {
            long sum = 0;
            for(int j = 0; j < x.length; j++) {
                if((i & (1 << j)) != 0) {
                    sum += x[j];
                }
            }
            ts.add((int)(sum % m));
        }
        return ts;
    }

 

标签:compute,int,S2,Maximum,Subsequence,CodeForce,ans,out,TreeSet
From: https://www.cnblogs.com/lz87/p/16744118.html

相关文章

  • Educational Codeforces Round 136 C-D
    D.ResetKEdges显然对于每个k每个答案具有单调性我们考虑二分一个能保留的最大长度x然后我们自上至下肯定不好操作我们考虑自下而上对于每次到达了我们最大长度x......
  • Codeforces Round #822 (Div. 2)
    Preface这场间隔有点久了,ABC是上周日打的,DE是这周四写的感觉这场难度海星,比DE都不会的823友好多了A.SelectThreeSticks容易发现最终变成的长度一定是已经存在的,因......
  • *Codeforces Round #235 (Div. 2) C. Team(贪心)
    https://codeforces.com/contest/401/problem/C题目大意:给定n个0,m个1;让我们构建出一个字符串满足:不能连续2个以上的0,不能出现3个连续的1;可以的话就输出任意正确的结......
  • Codeforces Round #823 (Div. 2)
    B.MeetingontheLine题意:有n个人,第i个人的坐标是xi,从xi移动到yi要花|xi -yi|的时间。除此之外,他还需要ti 的时间打扮。试求一点使得所有人到这里所花时间的最大......
  • Educational Codeforces Round 135 (Rated for Div. 2) - E. Red-Black Pepper
    exgcdProblem-E-Codeforces题意给\(n\;(n<=3*10^5)\)个菜,每个菜可以加红辣椒或黑辣椒,分别可以获得\(c[i],d[i]\)分;有\(m\;(m<=3*10^5)\)个商店,第i个商店包......
  • js find the maximum and minimum values in an array All In One
    jsfindthemaximumandminimumvaluesinanarrayAllInOnejs找出数组中的最大值与最小值AllInOnenumber/numberstringbuildinmethodsMath.max&Ma......
  • Codeforces Round #240 (Div. 1) B. Mashmokh and ACM(DP)
    https://codeforces.com/contest/414/problem/B题目大意:给定一个范围【1,k】,要求我们从这里面选出n个数字,并且满足任意两个相邻数字中后一个数字%前一个数字==0问我......
  • Codeforces Round #105 (Div. 2) D. Bag of mice
    CodeforcesRound#105(Div.2)翻译岛田小雅D.Bagofmice出题人Nickolas巨龙和公主在纠结大年夜应该干什么。巨龙想去山上看精灵们在月光下跳舞,但公主只想早点睡......
  • Codeforces Round #823 (Div. 2)(持续更新)
    Preface本来没准备打这场比赛的,因为昨天出去high玩了一整天才发现今天才发现晚上有CF,遂报名rush一发结果今天状态有点崩,先是B看错题导致浪费时间,然后又是D自己叉自己把原......
  • Codeforces Round #822 (Div. 2) - E. Rectangular Congruence
    同余Problem-E-Codeforces题意给一个长度为\(n(2<=n<350)\)的数组\(b_i\),\(0<=b_0,b_1...b_n<n\)要构造一个大小为\(n*n\)的矩阵A,\(a_{i,i}=b_i\),并且满......