888E - Maximum Subsequence 中途相遇法
Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
void setIO(string a){freopen((a+".in").c_str(),"r",stdin),freopen((a+".out").c_str(),"w",stdout);}
void shutIO(){ fclose(stdin),fclose(stdout);} #define ll long long
#define maxn 50
int arr[maxn],brr[maxn],p[maxn];
int A[5000000],B[5000000],cnta,cntb;
bool cmp1(int i,int j){return i<j;} //min->max
bool cmp2(int i,int j){return i>j;} //max->min
int main(){
//setIO("sweet");
int n,mod,l,r;
scanf("%d%d",&n,&mod);
l=n/2,r=n-l;
for(int i=1;i<=l;++i)scanf("%d",&arr[i]);
for(int i=1;i<=r;++i)scanf("%d",&brr[i]);
for(int i=1;i<=22;++i) p[i]=(1<<i);
for(int sta=1;sta<p[l];++sta){
int k=sta,len=1,sum=0;
while(k>0){
if(k&1) sum=((ll)(sum+arr[len])%mod);
++len;
k>>=1;
}
A[++cnta]=sum;
}
for(int sta=1;sta<p[r];++sta){
int k=sta,len=1,sum=0;
while(k>0){
if(k&1) sum=((ll)(sum+brr[len]))%mod;
++len;
k>>=1;
}
B[++cntb]=sum;
}
sort(A+1,A+1+cnta,cmp1);
sort(B+1,B+1+cntb,cmp2);
int i,j,ans=0;
for(i=1,j=1;i<=cnta&&j<=cntb;){
while(A[i]+B[j]>=mod && j<=cntb) ++j;
if(j>cntb) break;
while(A[i]+B[j]<mod && i<=cnta) ans=max(ans,A[i]+B[j]),++i;
if(i>cnta) break;
} sort(A+1,A+1+cnta,cmp2);
sort(B+1,B+1+cntb,cmp1);
for(i=1,j=1;i<=cnta&&j<=cntb;){
while(A[i]+B[j]>=mod && i<=cnta) ++i;
if(i>cnta) break;
while(A[i]+B[j]<mod && j<=cntb) ans=max(ans,A[i]+B[j]),++j;
if(j>cntb) break;
}
printf("%d",ans);
// shutIO();
return 0;
}
888E - Maximum Subsequence 中途相遇法的更多相关文章
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
Difference Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- 【UVALive】2965 Jurassic Remains(中途相遇法)
题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- LA 2965 中途相遇法
题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...
随机推荐
- CxImage动态加载图片(判断图片文件类型)
1.打开一张图可以通过创建一个新的CxImage对象来完成,通过构造函数来打开一张图CxImage::CxImage(const char * filename, DWORD imagetype)其中 ...
- fieldset 标签 -- 对表单进行分组
转自:https://xhmaomy-163-com.iteye.com/blog/1066977 fieldset——一个不常用的HTML标签 fieldset 标签 -- 对表单进行分组 在for ...
- Spring mvc <mvc:resources ***/> 作用
<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --> 如在页面需要导入其它 ...
- win-visualviewport-space
html.win-hoverable <div class="win-visualviewport-space"></div> <section cl ...
- springmvc两种非注解的处理器适配器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- LCD中如何描绘点阵数据
下载软件“液晶汉字点阵zimo21” 描绘数据 打开软件后,新建图像-取模方式选择C51(A51和C51区别就是,A-F开头要加0,例如0x0AF)-模拟动画中放大格点-描绘图像-点阵生成区 对获得数 ...
- python3 之 Ellipsis
在翻django 代码的时候无意中看到的, 主要还是在注解时候使用 官方参考:https://docs.python.org/3/library/constants.html#Ellipsis 注意: ...
- (WC2018模拟十二)【FJOI2016集训Day7T3】Xor-Mul棋盘
是不是应该第100篇博文纪念一下? 题解: 本质简单题...但是我没仔细看这题... 观察它的两个式子,都是xor完再乘以某个数,意味着d数组的每个二进制位对答案的贡献都是独立的,可以每一位分开处理. ...
- CSS核心原理
1.优先原则: 后解析的内容,会覆盖掉之前解析的内容: 同一个选择器:文件执行从上往下,后面的样式会覆盖前面的: 如下例中color最终为粉色: div { color:red; color:pink ...
- 【Paper Reading】Bayesian Face Sketch Synthesis
Contribution: 1) Systematic interpretation to existing face sketch synthesis methods. 2) Bayesian fa ...