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 ...
随机推荐
- mybatis在Mapper的xml文件中的转义字符的处理
XML转义字符 < < 小于号 > > 大于号 & & 和 ' ’ 单引号 " " 双引号 用转义字符进行替换 例如 SE ...
- nginx 状态监控
通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况.Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过web界面,一种是通过命令,web查看要比命令查看显示的 ...
- u3d局域网游戏网络(c# socket select 模型)——续
原文:http://www.cnblogs.com/saucerman/p/5555793.html 因为项目要加语音.语音数据都非常大.所以顺带就把之前写的网络模块一起测试了. 然后发现了一些bug ...
- 2016-2017 2 20155335《java程序设计》第四周总结
# 20155335 <Java程序设计>第四周学习总结 ## 教材学习内容总结 继承,在本职上是特殊到一般的关系,即is—a关系,子类继承父类,表明子类是一种特殊的父类,并且具 ...
- Problem L. Visual Cube(杭电多校2018年第三场+模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...
- 通过实例来学习XML DTD
使用DTD的原因: 注意:由于它自身的一些缺点,DTD终将被淘汰,但是它还是要学习的.学习完DTD后,后面继续学习XML Schema. 1,通过 DTD,您的每一个 XML 文件均可携带一个有关其自 ...
- mysql中的时间year/date/time/datetime
year: mysql> create table y(y year); Query OK, 0 rows affected (0.03 sec) mysql> desc y; +---- ...
- Ubuntu 14.04 ThinkPad E431无线网卡驱动安装
Ubuntu 14.04下安装无线网卡驱动. sudo apt-get install linux-headers-generic build-essential dkms sudo apt-get ...
- linux===进程操作
ps -ef ps -aux|grep chat.js a:显示所有程序 u:以用户为主的格式来显示 x:显示所有程序,不以终端机来区分 kill -9 nohup python da.py & ...
- python基础===python实现截图
python实现全屏截图: from PIL import ImageGrab im = ImageGrab.grab() im.save('F:\\12.png')