Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很多的时间。
Meet in the middle(有时候也叫作split and merge)是一种用以获取足够高效解决方案的灵巧的思想。和分治思想非常类似,它将问题分割成两个部分,然后试着合并这两个子问题的结果。好处在于通过使用一点额外的空间,你可以解决两倍规模的原来可以解决的问题。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set <ll> se[2];
const int maxn = 40;
ll num[maxn];
ll n,m;
void dfs(ll sum,ll l,ll r)
{
if(l == r)
{
se[r == n].insert(sum);
return ;
}
dfs((sum+num[l])%m,l+1,r);
dfs(sum,l+1,r);
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%lld",&num[i]);
num[i] = num[i] % m;
}
dfs(0,0,n/2);
dfs(0,n/2,n);
set<ll> ::iterator iter,iter2;
ll Max = 0;
for(iter=se[0].begin();iter!=se[0].end();iter++)
{
ll x = *iter;
ll y = m - 1 - x;
iter2 = se[1].upper_bound(y);
iter2--;
Max = max(Max,(x+*iter2)%m);
}
printf("%lld",Max);
}
Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)的更多相关文章
- 【CF888E】Maximum Subsequence(meet in the middle)
[CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...
- CF888E Maximum Subsequence(meet in the middle)
给一个数列和m,在数列任选若干个数,使得他们的和对m取模后最大( \(1<=n<=35\) , \(1<=m<=10^{9}\)) 考虑把数列分成两份,两边分别暴力求出所有的可 ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)
感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)
You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...
- Educational Codeforces Round 32 E. Maximum Subsequence
题目链接 题意:给你两个数n,m,和一个大小为n的数组. 让你在数组找一些数使得这些数的和模m最大. 解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...
- Educational Codeforces Round 32 Problem 888C - K-Dominant Character
1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...
随机推荐
- Yii2 之 UrlManager 实践 (一)
1. enablePrettyUrl yii2默认不支持类似 http://<domain>/site/error 的url格式,需要在config.php中启用 enablePrett ...
- JMeter--PerfMon Metrics Collector监控内存及CPU
1.需要准备的软件及插件 ServerAgent-2.2.1.zip JMeterPlugins-Standard-1.3.1.zip 2.jmeter上JMeterPlugins-Standard- ...
- C# 连接 Oracle,读取Blob字段数据,存到文件中去,包括pdf等等
代码如下,记得引入Oracle的dll using System; using System.Collections.Generic; using System.ComponentModel; usi ...
- Chess 模拟
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6114 Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋 ...
- puppeteer 中国区的使用
puppeteer 中国区的使用 [issues]https://github.com/GoogleChrome/puppeteer/issues/1426 两种方案 使用 cnpm .npmrc 中 ...
- sublime text less安装踩坑图文讲解(less无法生成css)
唉,怎么感觉做个前端几乎把所有的坑都踩遍了啊,别人按照网上安装了一遍就好使,我这里就死活不行. 先说一下我的问题:网上说的能安装的都按了,可是sublime就是不给我生成css文件,后来知道了,就是l ...
- scrapy安装遇到的Twisted问题
贴上大佬的博客地址:https://blog.csdn.net/a19990412/article/details/78849881 电脑一直在爆下面这一堆的信息 Command”c:\users\l ...
- JS判断两个对象相同属性的属性值是否相等
function isObjectValueEqual(a, b) { var aProps = Object.getOwnPropertyNames(a); var bProps = Object. ...
- jenkins SVN更改密码后出现的坑爹问题
1.前提 公司SVN账号密码和AD账号密码是绑定在一起的,为了保证代码检出总是最新,jenkins中做代码检查前总会从SVN中检出最新代码. 最近公司要求AD账户不得使用原始密码,更改密码后,jenk ...
- Linux if 命令判断条件总结
Linux if 命令判断条件总结Linux if命令 关于文件属性的判断式 -a 如果文件存在 -b 如果文件存在,且该文件是区域设备文件 -c 当file存在并且是字符设备文件时返回真 -d 当p ...