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 ...
随机推荐
- Makefile $@,$^,$ 作用
/* main.c */ #include "mytool1.h" #include "mytool2.h" int ...
- Mybatis xml 写sql如何判断集合的size
在mybtis的映射文件中判断集合大小 list.size 例子如下: <if test="groupIds != null and groupIds.size>0" ...
- 铺砖问题 (状态压缩dp)
问题描述: 给定m×n个格子,每个格子被染成了黑色或白色.现在要用1×2的砖块覆盖这些格子,要求快于快之间互相不重叠,且覆盖了所有白色的格子(用 . 表示),但不覆盖任意一个黑色的格子(用 x 表示) ...
- Winform GDI+
什么是GDI+ GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface). 因为应用程序不能直 ...
- jq_常用方法
//获取兄弟元素 $('.class').siblings() 当前元素所有的兄弟节点 $('.class').prev() 当前元素前一个兄弟节点 $('.class').prevaAll() 当前 ...
- linux下终端游戏
sl 一列火车 oneko 一只淘气的小猫
- Linux NAPI/非NAPI
本文主要介绍二层收包流程,包括NAPI与非NAPI方式: NAPI:数据包到来,第一个数据包产生硬件中断,中断处理程序将设备的napi_struct结构挂在当前cpu的待收包设备链表softnet_d ...
- Linux实际用户(组)ID,有效用户(组)ID,设置用户(组)ID
实际用户(组)ID: 标识用户是谁,这两个字段在登录时取自口令文件中的登录项. 有效用户(组)ID: 决定了对文件的访问权限,通常有效用户(组)ID等于实际用户(组)ID,谁运行有效ID就等于谁的实际 ...
- 【bzoj4459】JSOI2013丢番图
某JSOI夏令营出题人啊,naive! 你还是得学习个,搬这种原题不得被我一眼看穿? 求个n^2的约数除以二,向上取整. #include<bits/stdc++.h> using nam ...
- DXEditingRow的错误原因
原因之一:例如commbox理由id这一列但是数据库表中没有的话就会报这个错误