CodeForces888E Maximum Subsequence(折半枚举+two-pointers)
题意
给定一个包含$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)的更多相关文章
- 【CF888E】Maximum Subsequence 折半搜索
[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 888E:Maximum Subsequence(枚举,二分)
You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...
- CF 888E Maximum Subsequence——折半搜索
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
- [Codeforces888E]Maximum Subsequence(暴力+meet-in-the-middle)
题意:给定n.m.有n个数,选出若干数加起来对m取模,求最大值 n<=35 如果直接暴力就是235,会T, 这里用到一个思想叫meet-in-the-middle, 就是把数列分成两半分别搜索, ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- Codeforces 888E Maximum Subsequence
原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
随机推荐
- 数学:BSGS
先来稍微回顾一下,我们已经会求模线性方程(包括其特殊情况乘法逆元) 我们还会进行幂取模的快速算法(模是质数用费马小定理,模一般情况用欧拉定理) 对于幂中指数特别大的情况,我们还延伸出了拓展欧拉定理来解 ...
- Windows/Linux javac/java编译运行引入所需的jar包
> Windows 假设要引用的jar放在D:/test目录下,名字为t1.jar, java源文件放在D:/test/src目录下,名字为t2.java. 编译: javac -cp d: ...
- 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...
- Linux while 和 read 的用法
Reference: [ linux man doc ] [ CSDN roler_ ] [ Reads from the file descriptor] read 命令说明 SYNTAX : re ...
- 打印菱形(c语言)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { // 定 ...
- js中三种定义变量 const, var, let 的区别
js中三种定义变量的方式const, var, let的区别 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 ...
- HTML/CSS/JS编码规范
最近整理了一份HTML/CSS/JS编码规范,供大家参考.目录:一.HTML编码规范二.CSS编码规范三.JS编码规范 一.HTML编码规范 1. img标签要写alt属性 根据W3C标准,img标签 ...
- Android应用程序App应用上线流程
对于很多初级开发者,可能对app应用上线不太了解,本文跟大家介绍一下怎么上线app应用.上线App并不是一件很困难的事情,App的应用功能也不需要很强大,甚至不用联网,只有简单的一两个页面的App应用 ...
- 关于ssh三大框架整合的碎碎念
三大框架整合,无非就是一个导jar包,修改配置文件的过程.完了就没事了. 还是有很多细节性的问题 比如在spring中写applicationContext.xml文件时不提示: 解决方法如下: 如果 ...
- pandas中DataFrame使用
切片选择 #显示第一行数据print(df.head(1)) #显示倒数三行数据 print(df.tail(3)) loc df.loc[row_index,col_index] 注意loc是根 ...