zjnu1745 DOMINE (状压dp+1*2铺砖)
Description
Mirko has a chessboard with N rows and just three columns. Slavica has written an integer on each field. Mirko has K dominoes at his disposal, their dimensions being 2x1, and has to arrange all of them on the board without overlapping, in a way that each
domino covers exactly two fields of the board. He can rotate the dominoes as he pleases.
Help Mirko cover the largest sum of numbers possible with the dominoes!
Input
The first line of input contains the integer N (1 ≤ N ≤ 1000), the number of rows, and K (1 ≤ K ≤ 1000), the number of dominoes available. Each of the following N lines contains three integers written in the ith row of the board. All numbers will be lesser
than 10^6 by absolute value
Output
The first and only line of output must contain the maximal sum possible to cover with exactly K dominoes.
Sample Input
2 1 -1
1 3 2
0 2 3
2 1 1
3 3 0
Sample Output
16
题意:给你一个n*3的矩阵,每一个格子都有对应的价值,让你在里面恰好铺满k块1*2的砖,可以竖着铺,也可以横着铺,问铺完k块砖后所能覆盖到的最大价值是多少。
思路:与poj2411全部铺满不同,这题可以不铺满,所以我们可以考虑每一层状态里0代表空缺没有铺,1代表这个格子铺过了,那么对于每一层,我们可以枚举这一层铺的状态,如果这一层有格子是竖着放的,那么我们要看看上一层的当前这个位置是不是0,如果是0就可以竖着放,我们枚举每一层的状态的时候,只考虑铺在当前这一行的格子以及可能占用上一层的砖,不考虑延伸到下一层的砖。这样我们就可以设dp[i][j][state]表示当前为i行,已经铺了k个砖,当前行的状态为state的能覆盖到的最大价值。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 1100000000
#define pi acos(-1.0)
#define maxn 1005
int dp[2][maxn][10]; //滚动数组,dp[i][j]表示前i行,选了j个,当前行状态为state的最大总价值
int sczt[20]={0,7,6,4,5,4,1,0,3,2,0,1,0}; //这里是上一层的状态
int jia[20]= {0,3,2,2,2,1,2,1,2,1,1,1,0}; //这里是用这个状态铺砖时,增加了多少砖块
int zt[20]= {0,7,6,7,5,4,7,6,3,2,3,1,0}; //这里是当前行的状态
int a[maxn][maxn];
int getsum(int h,int idx) //这里算的是增加多少价值
{
int i,j;
if(idx==1)return a[h][1]+a[h][2]+a[h][3]+a[h-1][1]+a[h-1][2]+a[h-1][3];
if(idx==2)return a[h][1]+a[h][2]+a[h-1][1]+a[h-1][2];
if(idx==3)return a[h][1]+a[h][2]+a[h][3]+a[h-1][1];
if(idx==4)return a[h][1]+a[h][3]+a[h-1][1]+a[h-1][3];
if(idx==5)return a[h][1]+a[h-1][1];
if(idx==6)return a[h][1]+a[h][2]+a[h][3]+a[h-1][3];
if(idx==7)return a[h][1]+a[h][2];
if(idx==8)return a[h][2]+a[h][3]+a[h-1][2]+a[h-1][3];
if(idx==9)return a[h][2]+a[h-1][2];
if(idx==10)return a[h][2]+a[h][3];
if(idx==11)return a[h][3]+a[h-1][3];
if(idx==12)return 0;
}
int main()
{
int n,k,i,j,len,val,state,t;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=1;i<=n;i++){
for(j=1;j<=3;j++){
scanf("%d",&a[i][j]);
}
}
for(j=1;j<=k;j++){
for(state=0;state<8;state++){
dp[1][j][state]=-inf;
}
}
dp[1][0][0]=0;
dp[1][1][6]=a[1][1]+a[1][2];
dp[1][1][3]=a[1][2]+a[1][3];
int tot=1;
for(i=2;i<=n;i++){
for(j=0;j<=k;j++){
for(state=0;state<8;state++){
dp[1^tot][j][state]=-inf;
}
}
for(j=0;j<=k;j++){
for(t=1;t<=12;t++){ //这里枚举的是12种这一行与上一行可能的状态,即第一个格子里竖着放,第一个格子横着放,第一个格子不放等状态
for(state=0;state<8;state++){ //枚举上层状态
if((sczt[t]&state)==0 ){
if(j+jia[t]<=k){
dp[1^tot][jia[t]+j ][zt[t] ]=max(dp[1^tot][jia[t]+j ][zt[t] ],dp[tot][j][state]+getsum(i,t) );
}
}
}
}
}
tot=1^tot;
}
int maxx=dp[tot][k][0];
for(state=1;state<8;state++){
maxx=max(maxx,dp[tot][k][state] );
}
printf("%d\n",maxx);
}
return 0;
}
zjnu1745 DOMINE (状压dp+1*2铺砖)的更多相关文章
- 状压DP天秀
状压DP,依靠的是把状态用某种压缩方式表示出来进而DP,大多数时候是二进制状压. 直接看例题吧. 一双木棋 九尾狐吃棉花糖 islands and bridges 愤怒的小鸟 芯片 ...
- 【XSY2745】装饰地板 状压DP 特征多项式
题目大意 你有\(s_1\)种\(1\times 2\)的地砖,\(s_2\)种\(2\times 1\)的地砖. 记铺满\(m\times n\)的地板的方案数为\(f(m,n)\). 给你\(m, ...
- poj1038 Bugs Integrated,Inc. (状压dp)
题意:N*M的矩阵,矩阵中有一些坏格子,要在好格子里铺2*3或3*2的地砖,问最多能铺多少个. 我的方法好像和网上流传的方法不太一样...不管了.... 由数据范围很容易想到状压dp 我们设某个状态的 ...
- 铺地砖|状压DP练习
有一个N*M(N<=5,M<=1000)的棋盘,现在有1*2及2*1的小木块无数个,要盖满整个棋盘,有多少种方式?答案只需要mod1,000,000,007即可. //我也不知道这道题的来 ...
- dp,状压dp等 一些总结
也就作业几题而已,分析一下提醒 最重要的就是,记住,没用的状态无论怎么转移最后都会是没用的状态,所以每次转移以后的有值的状态都是有用的状态. 几种思考方向: 第一种:枚举当前的状态,转移成另外一个状态 ...
- 【BZOJ2064】分裂 状压DP
[BZOJ2064]分裂 Description 背景:和久必分,分久必和...题目描述:中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力.同时经常搞OI的他把这个变成了一个 ...
- poj2411 Mondriaan's Dream[简单状压dp]
$11*11$格子板上铺$1*2$地砖方案.以前做过?权当复习算了,毕竟以前学都是浅尝辄止的..常规题,注意两个条件:上一行铺竖着的则这一行同一位一定要铺上竖的,这一行单独铺横的要求枚举集合中出现连续 ...
- POJ 2411 Mondriaan's Dream ——状压DP 插头DP
[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...
- [Poj2411]Mondriaan's Dream(状压dp)(插头dp)
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18096 Accepted: 103 ...
随机推荐
- Java虚拟机常用的性能监控工具
基础故障处理工具 jps: 虚拟机进程状况工具 功能:来处正在运行的虚拟机进程,并显示虚拟机执行主类名称,以及本地虚拟机唯一ID. 它是使用频率最高的命令行工具,因为其他JDK工具大多需要输入他查询到 ...
- 关于使用th:text获取不到值
今天在使用thymeleaf模板引擎整合SpringBoot时,对于从controller层传递过来的参数"message",无法获取. 控制层代码如下: @PostMapping ...
- 入门OJ:Coin
题目描述 你有n个硬币,第i硬币面值为ai,现在总队长想知道如果丢掉了某个硬币,剩下的硬币能组成多少种价值?(0价值不算) 输入格式 第一行一个整数n 第二行n个整数.,a1,a2-an. 1< ...
- IP2723T中文规格书PDF
IP2723T 是一款集成多种协议.用于 USB 输出端口的快充协议 IC.支持多种快充协议,包括 USBTypeC DFP,PD2.0/PD3.0/PPS,HVDCPQC4/QC4+/QC3.0/Q ...
- 容器化安装Mysql 8.0 并部署主从复制
系统: Centos 7.4 数据库版本:8.0.20 两台机器做相同操作 安装Docker export VERSION=18.06 && curl -fsSL http://rai ...
- MySQL调优性能监控之performance schema
一.performance_schema的介绍 performance:性能 schema:图(表)示,以大纲或模型的形式表示计划或理论. MySQL的performance schema 用于监控M ...
- python RecursionError: maximum recursion depth exceeded while calling
import copyimport sys # 导入sys模块sys.setrecursionlimit(8192) # 将默认的递归深度修改为r = sys.getrecursionlimit()_ ...
- libco hook原理简析
我们先看一下libco协程库的特性描述 libco的特性 无需侵入业务逻辑,把多进程.多线程服务改造成协程服务,并发能力得到百倍提升; 支持CGI框架,轻松构建web服务(New); 支持gethos ...
- css知识补充
盒子模型的介绍: 在网页中基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型 盒子模型的五个属性: width,height,border(边框),padding(内边距),margin(外 ...
- mysql中int型的数字怎么转换成字符串
字段:number 是integer类型 在表test中 select cast(number as char) as number from test; 或者convert()方法.因为转换 ...