Codeforces-888E:Maximum Subsequence(思维)
来源:互联网 发布:实名淘宝号购买 编辑:程序博客网 时间:2024/05/21 08:05
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, ..., bk (1 ≤ b1 < b2 < ... < bk ≤ n) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of .
The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print the maximum possible value of .
4 45 2 4 1
3
3 20199 41 299
19
In the first example you can choose a sequence b = {1, 2}, so the sum 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<=20,可以考虑状态压缩,但n>=35。那么可以考虑把n个数分为2个部分来状态压缩,然后用vector<int>c储存第一部分各个状态模m后的值,然后枚举第二部分各个状态模m后的值y,那我们可以在c中二分查找m-y-1,如果找不到,可以考虑比m-y-1小的最大的数和比m-y-1大的最大的数,这样就能保证答案尽可能的靠近m-1。
#include<bits/stdc++.h>using namespace std;__int64 a[40],b[40],n,m;vector<__int64>c;__int64 aget(__int64 x){ __int64 ans=0; for(__int64 i=0;i<n/2;i++,x/=2)if(x%2)ans+=a[i]; return ans%m;}__int64 bget(__int64 x){ __int64 ans=0; for(__int64 i=0;i<n-n/2;i++,x/=2)if(x%2)ans+=b[i]; return ans%m;}int main(){ __int64 ans=0; scanf("%I64d%I64d",&n,&m); for(__int64 i=0;i<n/2;i++)scanf("%I64d",&a[i]),ans=max(ans,a[i]%m); for(__int64 i=0;i<n-n/2;i++)scanf("%I64d",&b[i]),ans=max(ans,b[i]%m); for(__int64 i=0;i<(1<<(n/2));i++)c.push_back(aget(i)); sort(c.begin(),c.end()); for(__int64 i=0;i<(1<<(n-n/2));i++) { __int64 y=bget(i); ans=max(ans,y); __int64 x=lower_bound(c.begin(),c.end(),m-y-1)-c.begin(); if(c.size()>0) { if(x>=0&&x<c.size())ans=max(ans,(y+c[x])%m); if(x>0)ans=max(ans,(y+c[x-1])%m); ans=max(ans,(y+c[c.size()-1])%m); } } cout<<ans<<endl; return 0;}
- Codeforces-888E:Maximum Subsequence(思维)
- codeforces 888E Maximum Subsequence
- codeforces 888 E. Maximum Subsequence(好题)
- codeforces 888E Maximum Subsequence (折半枚举 双向搜索)
- codeforces Educational Codeforces Round 32 E. Maximum Subsequence
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
- CF 888E Maximum Subsequence 折半搜索.
- Lexicographically Maximum Subsequence CodeForces
- Maximum Subsequence CodeForces
- Codeforces 509E(思维)
- CodeForces 375B Maximum Submatrix 2(思维)
- codeforces 217E 逆向思维
- Codeforces 230E Triangles【思维】
- Codeforces 124c Lexicographically Maximum Subsequence
- CodeForces 197C Lexicographically Maximum Subsequence
- codeforces 197C Lexicographically Maximum Subsequence
- Educational Codeforces Round 7(E)贪心,思维
- Codeforces Round #403 E. Underground Lab(思维+搜索)
- JQuery练习删除
- for循环语句
- 关于x264编码帧,单个nalu还是多个nalu的问题
- linux下修改数据库MySql密码
- json类型的细研
- Codeforces-888E:Maximum Subsequence(思维)
- HDFS格式化错误 SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException
- [14-02]TCP/IP
- python清空idle的内容
- 数组指针和指针数组
- Xcode9.1无线真机调试
- 对:同步与异步、阻塞与非阻塞的理解(通俗易懂)
- MongoDb学习笔记-2
- test