题意

给定一个包含$n$个数的序列$a$,在其中任选若干个数,使得他们的和对$m$取模后最大。($n\leq 35$)

题解

显然,$2^n$的暴枚是不现实的...,于是我们想到了折半枚举,分成两部分暴枚,然后考虑合并,合并的时候用two-pointers思想扫一遍就行了。

其实这是一道折半枚举+Two-Pointers的很好的练手题

//最近CodeForces有点萎,可能会JudgementError,但已经评测过了,能AC,多交几次应该可以
#include <cstdio>
#include <algorithm>
using std::max;
using std::sort; const int N = 40, K = 19;
int n, m, k, ans, totx, toty;
long long a[N], x[1 << K], y[1 << K]; template <typename T>
inline void read(T &x) {
x = 0; char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
} void dfsx (int i, long long sum) {
if (i > k) { x[++totx] = sum % m; return ; }
dfsx (i + 1, sum + a[i]);
dfsx (i + 1, sum);
} void dfsy (int i, long long sum) {
if (i > n) { y[++toty] = sum % m; return ; }
dfsy (i + 1, sum + a[i]);
dfsy (i + 1, sum);
} int main () {
read(n), read(m), k = n >> 1;
for (int i = 1; i <= n; ++i) read(a[i]);
dfsx(1, 0), dfsy(k + 1, 0);
sort(&x[1], &x[totx + 1]), sort(&y[1], &y[toty + 1]);
int l = 1, r = toty;
while (l <= totx) {
while (r && x[l] + y[r] >= m) --r; if(!r) break;
ans = max(ans, int((x[l] + y[r]) % m)), ++l;
}
printf ("%d\n", ans);
return 0;
}

CodeForces888E Maximum Subsequence(折半枚举+two-pointers)的更多相关文章

  1. 【CF888E】Maximum Subsequence 折半搜索

    [CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...

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

    E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Codeforces 888E:Maximum Subsequence(枚举,二分)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  4. CF 888E Maximum Subsequence——折半搜索

    题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...

  5. [Codeforces888E]Maximum Subsequence(暴力+meet-in-the-middle)

    题意:给定n.m.有n个数,选出若干数加起来对m取模,求最大值 n<=35 如果直接暴力就是235,会T, 这里用到一个思想叫meet-in-the-middle, 就是把数列分成两半分别搜索, ...

  6. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))

    888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  7. Codeforces 888E Maximum Subsequence

    原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...

  8. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  9. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

随机推荐

  1. [洛谷P2577] [ZJOI2005]午餐

    洛谷题目链接:[ZJOI2005]午餐 题目描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的 ...

  2. [洛谷P2596] [ZJOI2006]书架

    洛谷题目链接:书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后 ...

  3. PowerDesigner16 活动图

    活动是某件事情正在进行的状态.活动在状态机中表现为一个由一系列动作组成 的非原子的执行过程. 活动图是一种描述系统行为的图,它用于展现 参与行为的实体所进行的各种活动的顺序关系.活动图(Activit ...

  4. WCF 同一个解决方案中控制台应用添加服务引用报错

    错误提示: “Unable to check out the current file. The file may be read-only or locked, or you may need to ...

  5. 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题

    最近在使用静态库时,总是出现这个问题.下面总结一下我得解决方法: 1. .m文件没有导入    在Build Phases里的Compile Sources 中添加报错的文件 2. .framewor ...

  6. Spring Boot提供的特性

    一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...

  7. js中的apply、call、bind

    每个函数都包含两个非继承而来的方法,call()和apply(),可以改变函数内部this的指向 1.apply 用另一个对象替换当前对象,接收两个参数,第一个参数表示需要绑定的this变量,第二个参 ...

  8. Git常规配置与基本用法

    Git环境配置 一. 全局配置 1. 配置文件 git全局配置文件.gitconfig默认在当前系统用户文件夹下,window可运行%USERPROFILE%查找,Mac系统在cd ~查找. 具体配置 ...

  9. java===java基础学习(9)---方法参数

    方法参数注意三要点: 一个方法不能修改一个基本数据类型的参数(数值型或者布尔型). 一个方法可以改变一个对象参数的状态. 一个方法不能让对象参数引用一个新的对象. package testbotoo; ...

  10. Linux时间子系统之一:clock source(时钟源)【转】

    转自:http://blog.csdn.net/droidphone/article/details/7975694 clock source用于为linux内核提供一个时间基线,如果你用linux的 ...