zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧

有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方

所以用状态压缩表示前面有的序列组合,找到了符合的,就往上累加合并生成新状态,否则就添加到前面的状态的后面构成新状态,因为每一个的状态都由前一个所得,用滚动数组即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[][*];
int A[];
int n;
int main()
{
int ti;
scanf("%d",&ti);
while (ti--)
{
memset(dp,-,sizeof dp);
scanf("%d",&n);
for (int i=; i<=n; i++)
{
scanf("%d",&A[i]);
}
int pos=;
int ans=;
dp[][]=;
for (int i=; i<=n; i++)
{
for (int j=; j<*; j++)
{
if (dp[pos^][j]==-) continue;
dp[pos][j]=max(dp[pos][j],dp[pos^][j]);
ans=max(ans,dp[pos][j]);
int t=j;
int q=A[i]-;
int sum=A[i];
if ((t&q)==)
{
int k=A[i];
while ((t&k))
{
sum+=k<<;
k<<=;
}
t&=~(k-);
t|=k;
}
else t=A[i];
dp[pos][t]=max(dp[pos][t],dp[pos^][j]+sum);
ans=max(ans,dp[pos][t]);
}
pos^=;
}
printf("%d\n",ans);
}
}

最新文章

  1. HTML5标签嵌套规则
  2. [css3]CSS3选择器:nth-child和:nth-of-type之间的差异
  3. Oracle 索引&lt;七&gt;
  4. 一个相当好的状态机(DFA, 确定有限状态机)的编码实现,相当简洁漂亮
  5. nopCommerce架构分析系列(二)数据Cache
  6. Java 浮点型与双精度数值比较
  7. API设计中响应数据格式用json的优点
  8. 第5章 支持和咨询选项 - Identity Server 4 中文文档(v1.0.0)
  9. token的设置与获取
  10. java中的定时任务小示例
  11. 【Wannafly挑战赛14C可达性】【Tarjan缩点】
  12. 01炼数成金TensorFlow基本概念
  13. iOS push新的调用方法
  14. poj2054
  15. clr_zmq Vs2010版本
  16. 设计模式php篇(一)————单例模式
  17. elk系列1之入门安装与基本操作【转】
  18. 利用html5制作一个时钟动画
  19. win 10 用户上传头像保存的文件夹路径
  20. Makefile 调试

热门文章

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示
  2. Linux系统在IT行业处于什么位置
  3. 第1节 Scala基础语法:11、映射;12、元组
  4. Golang mysql数据库
  5. ABC155F - Perils in Parallel
  6. Codeforces 1304D. Shortest and Longest LIS
  7. Python - ORM(数据库相关)
  8. gcc/g++/make/cmake/makefile/cmakelists的恩恩怨怨
  9. BZOJ 4166: 月宫的符卡序列
  10. Victor and String[Bestcoder #52 1004](回文树)