【Link】:

【Description】



有4堆书;

每本书编号从1..20

每堆书都是N本;

然后每次只能从任意一堆的堆顶拿一本书装到自己的口袋里;

你的口袋最多容纳5本书;

当你的口袋里有两本一样的书的时候,那一对书就归你了;

但是一旦你的口袋装满了,就不能再装书了;游戏停止

问你最多能拿多少对书。

【Solution】



比较明显的动规了;

每次只能从4堆书的堆顶中选择一本书

定义f[i][j][k][l]表示第一堆书上有i本数,第二堆书。。。。获得的最大书对数;

则从某一堆书上拿一本书;

相当于某一维变量的值减去1;

写个前缀和,记录最初始每一堆书的第j本书上面1..20这些书各有多少本

转移的时候,就能根据这个前缀和,获取之前拿了哪些书了;

成对的消掉,增加数目,不能成对的,则记录书的类型;

对于拿的书的类型<5的状态做转移就好;



【NumberOf WA】



0



【Reviw】

【Code】

#include <bits/stdc++.h>
using namespace std; const int N = 40;
const int INF = 0x3f3f3f3f; int n,x[5][N+10],f[N+5][N+5][N+5][N+5],pre[5][N+10][21];
int temp[22]; pair<int,int> tongji(int a,int b,int c,int d){
memset(temp,0,sizeof temp);
for (int i = 1;i <= 20;i++){
temp[i] += pre[1][a][i];
temp[i] += pre[2][b][i];
temp[i] += pre[3][c][i];
temp[i] += pre[4][d][i];
}
int num = 0,num1 = 0;
for (int i = 1;i <= 20;i++){
num+=temp[i]/2;
temp[i]%=2;
num1+=temp[i];
}
return make_pair(num1,num);
} int main(){
//freopen("F:\\rush.txt","r",stdin);
while (~scanf("%d",&n) && n){
for (int i = n;i >= 1;i--)
for (int j = 1;j <= 4;j++)
scanf("%d",&x[j][i]); for (int i = 1;i <= 4;i++){
for (int k = 1;k <= 20;k++) pre[i][n+1][k] = 0;
for (int j = n;j >= 1;j--){
for (int k = 1;k <= 20;k++)
pre[i][j][k] = pre[i][j+1][k];
pre[i][j][x[i][j]]++;
}
} memset(f,-INF,sizeof f);
f[n][n][n][n] = 0;
int ans = 0;
pair <int,int> pii;
for (int i = n;i >= 0;i--)
for (int j = n;j >= 0;j--)
for (int k = n;k >= 0;k--)
for (int l = n;l >= 0;l--)
if (f[i][j][k][l]>=0){
ans = max(ans,f[i][j][k][l]);
if (i){
pii = tongji(i,j+1,k+1,l+1);
if (pii.first<5){
int &t = f[i-1][j][k][l];
t = max(t,pii.second);
}
}
if (j){
pii = tongji(i+1,j,k+1,l+1);
if (pii.first<5){
int &t = f[i][j-1][k][l];
t = max(t,pii.second);
}
}
if (k){
pii = tongji(i+1,j+1,k,l+1);
if (pii.first<5){
int &t = f[i][j][k-1][l];
t = max(t,pii.second);
}
}
if (l){
pii = tongji(i+1,j+1,k+1,l);
if (pii.first<5){
int &t = f[i][j][k][l-1];
t = max(t,pii.second);
}
}
}
printf("%d\n",ans);
}
return 0;
}

【Uva 10118】Free Candies的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. HDU 3652 B-number(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP G - B-number 题意 求1-n的范围里含有13且能被13整除的数字的个数. 思路 首先,了解这样一个式子:a%m == ((b%m)* ...

  2. angularjs $http 服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  3. 图像几何变换(geometric transformation)

    1. imwarp B = imwarp(A,tform) demo I = imread('cameraman.tif'); tform = affine2d([1 0 0; .5 1 0; 0 0 ...

  4. Autofac依赖注入框架

    最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...

  5. User-defined types

    We have used many of Python’s built-in types; now we are going to define a new type. As an example, ...

  6. 虚拟机下安装mysql

    虚拟机下CentOS6.8下安装MYSQL5.6 方法: 整理修改于 http://www.cnblogs.com/liuyi2614/p/6382183.html 开始时: 普通用户是$ root用 ...

  7. links[v1]

    justep core java Spring Boot ui5 template spring Cross-origin resource sharing 统一异常处理 数据库连接池的选择 Drui ...

  8. WebSocket handshake: Unexpected response code: 404

    在执行    http://www.cnblogs.com/best/p/5695570.html  提供的 websocket时候, 报错了 “WebSocket handshake: Unexpe ...

  9. sql笔试题-1

    在oracle下sql:比较巧妙地是group by 部分 E from (select a.team,b.y from nba a,nba b ) c group by (c.y-rownum) o ...

  10. php基础:define()定义常数函数

    define(); 常量类似变量,不同之处在于: 在设定以后,常量的值无法更改 常量名不需要开头的美元符号 ($) 作用域不影响对常量的访问 常量值只能是字符串或数字 <?php define( ...