ZOJ 3802 Easy 2048 Again 状态DP
2024-10-08 14:44:53
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);
}
}
最新文章
- HTML5标签嵌套规则
- [css3]CSS3选择器:nth-child和:nth-of-type之间的差异
- Oracle 索引<;七>;
- 一个相当好的状态机(DFA, 确定有限状态机)的编码实现,相当简洁漂亮
- nopCommerce架构分析系列(二)数据Cache
- Java 浮点型与双精度数值比较
- API设计中响应数据格式用json的优点
- 第5章 支持和咨询选项 - Identity Server 4 中文文档(v1.0.0)
- token的设置与获取
- java中的定时任务小示例
- 【Wannafly挑战赛14C可达性】【Tarjan缩点】
- 01炼数成金TensorFlow基本概念
- iOS push新的调用方法
- poj2054
- clr_zmq Vs2010版本
- 设计模式php篇(一)————单例模式
- elk系列1之入门安装与基本操作【转】
- 利用html5制作一个时钟动画
- win 10 用户上传头像保存的文件夹路径
- Makefile 调试
热门文章
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示
- Linux系统在IT行业处于什么位置
- 第1节 Scala基础语法:11、映射;12、元组
- Golang mysql数据库
- ABC155F - Perils in Parallel
- Codeforces 1304D. Shortest and Longest LIS
- Python - ORM(数据库相关)
- gcc/g++/make/cmake/makefile/cmakelists的恩恩怨怨
- BZOJ 4166: 月宫的符卡序列
- Victor and String[Bestcoder #52 1004](回文树)