Codeforces 888E:Maximum Subsequence(枚举,二分)
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices \(b_1, b_2, ..., b_k (1 ≤ b_1 < b_2 < ... < b_k ≤ n)\) in such a way that the value of \(\sum^{k}_{i=1}a_{b_i}\) is maximized. Chosen sequence can be empty.
Print the maximum possible value of \(\sum^{k}_{i=1}a_{b_i}\).
Input
The first line contains two integers \(n\) and \(m (1 ≤ n ≤ 35, 1 ≤ m ≤ 10^9)\).
The second line contains \(n\) integers \(a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9)\).
Output
Print the maximum possible value of \(\sum^{k}_{i=1}a_{b_i}\).
Examples
Input
4 4
5 2 4 1
Output
3
Input
3 20
199 41 299
Output
19
Note
In the first example you can choose a sequence \(b = \{1, 2\}\), so the sum \(\sum^{k}_{i=1}a_{b_i}\) is equal to \(7\) (and that's \(3\) after taking it modulo \(4\)).
In the second example you can choose a sequence \(b = \{3\}\).
题意
给出\(n\)个数,从这\(n\)个数中选出几个数(可以不选),使得这些数的和对\(m\)取余后的值最大
思路
首先有一种特别暴力的方法:枚举出所有的状态后,找出对\(m\)取模后的最大值,时间复杂度\(O(2^n)\),这里\(n=35\),肯定是不行的
我们可以将这些数分成两段,分别枚举出这两段的所有状态,对左右两段排序,去重。然后从左半段中选出一个值\(value\),因为是对\(m\)取模后的最大值,所以最大的结果等于\(m-1\),在右半段利用二分查找大于\(m-1-value\)的位置\(place\),右半段\(place-1\)位置的数就是符合要求的数,相加取最大值即可
时间复杂度:\(O(2^{\left \lceil \dfrac {n}{2} \right \rceil}+n)\)
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int a[maxn];
int Left[maxn];
int Right[maxn];
int cntl,cntr;
int n,m;
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i],a[i]%=m;
int res=0;
int l,r;
l=r=n/2;
for(int i=0;i<(1<<r);i++)
{
res=0;
for(int j=0;j<r;j++)
if(i>>j&1)
res+=a[j],res%=m;
Left[cntl++]=res;
}
res=0;
r=n;
int num=r-l+1;
for(int i=0;i<(1<<num);i++)
{
res=0;
for(int j=0;j<num;j++)
if(i>>j&1)
res+=a[l+j],res%=m;
Right[cntr++]=res;
}
Left[cntl++]=0;
Right[cntr++]=0;
sort(Left,Left+cntl);
sort(Right,Right+cntr);
cntl=unique(Left,Left+cntl)-Left;
cntr=unique(Right,Right+cntr)-Right;
int ans=0;
for(int i=0;i<cntl;i++)
{
int res=m-Left[i]-1;
int pos=upper_bound(Right,Right+cntr,res)-Right;
int num=Right[pos-1];
ans=max(ans%m,(num+Left[i])%m);
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
Codeforces 888E:Maximum Subsequence(枚举,二分)的更多相关文章
- 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 ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- codeforces 880E. Maximum Subsequence(折半搜索+双指针)
E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 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 ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 497B Tennis Game( 枚举+ 二分)
B. Tennis Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- CF 888E Maximum Subsequence
一道比较套路的题,看到数据范围就差不多有想法了吧. 题目大意:给一个数列和\(m\),在数列任选若干个数,使得他们的和对\(m\)取模后最大 取膜最大,好像不能DP/贪心/玄学乱搞啊.\(n\le35 ...
- CF 888E Maximum Subsequence——折半搜索
题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...
- 888E - Maximum Subsequence 中途相遇法
Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...
随机推荐
- LeetCode1579题——圆圈中最后剩下的数字
1.题目描述:0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字.例如,0.1.2.3.4这5个数字组成一个圆圈,从数字0开始每次删 ...
- Linux学习 - 正则表达式
一.正则表达式与通配符 正则表达式:在文件中匹配符合条件的字符串,正则是包含匹配 通配符:用来匹配符合条件的文件名,通配符是完全匹配 二.基础正则表达式 元字符 作用 a* a有0个或任意多个 . 除 ...
- InnoDB的行锁模式及加锁方法
MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...
- 4.Vue.js-起步
Vue.js 起步 每个 Vue 应用都需要通过实例化 Vue 来实现. 语法格式如下: var vm = new Vue({ // 选项 }) 接下来让我们通过实例来看下 Vue 构造器中需要哪些内 ...
- 【C/C++】习题3-2 分子量/算法竞赛入门经典/字符串
给出一种物质的分子式,求分子量.只包含4种原子,分别为C,H,O,N. [知识点] 1.ASCII码 [阿拉伯数字]48~57 [大写字母]65~90 [小写字母]97~122 2.输入循环到n-1的 ...
- 03 - Vue3 UI Framework - 首页
顶部边栏做完了,接下来开始做官网的首页 返回阅读列表点击 这里 创建视图文件夹 让我们先新建一个 src/views 文件夹,用来存放官网的主要视图 然后在该文件夹下新建两个 vue 文件,作为我们的 ...
- C# 编写一个小巧快速的 Windows 动态桌面软件
开源自己前段时间使用 C# 编写的 Windows 动态桌面软件,在接下来的博客我将描写一些技术细节和遇到的一些坑.这个软件可以把视频设置成桌面背景播放,不仅如此而且还可以把网页或一个网页文件设置成桌 ...
- pipeline脚本管理
目录 一.代码仓库 二.远程拉取 一.代码仓库 1.使用gitlab做pipeline脚本的存储,新建一个仓库 2.新建文件,把代码放进去 脚本名可以按照规律填写,环境_应用名_类型,例如:test_ ...
- 转: iPhone屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- ciscn_2019_en_3
例行检查我就不放了,64位的程序放入ida中 可以看到s到buf的距离是0x10,因为puts是遇到\x00截止.而且题目没有限制我们s输入的数量,所以可以通过这个puts泄露出libc的基值 很明显 ...