T1:三取方格数

题目描述

设有N*N的方格图,我们将其中的某些方格填入正整数,
而其他的方格中放入0。
某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角。
在走过的路上,他取走了方格中的数。(取走后方格中数字变为0)
此人从左上角到右下角共走3次,试找出3条路径,使得取得的数总和最大。

输入

第一行:N (4<=N<=20)
接下来一个N*N的矩阵,矩阵中每个元素不超过80,不小于0

输出

一行,表示最大的总和。

样例输入

4
1 2 3 4
2 1 3 4
1 2 3 4
1 3 2 4

样例输出

39
 
题解:
F[i][j][k][g]表示一共走了i步,第1,2,3条路线的行坐标 知道行坐标 列坐标就是i-x+1
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-,ch=getchar();
return str;
}
int f[N*][N][N][N],a[N][N];
int main()
{
//freopen("pp.in","r",stdin);
int n=gi();
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
a[i][j]=gi();
int tmp=,from,to;
for(int i=;i<n+n;i++)
{
from=i-n+>?i-n+:;
to=i<n?i:n;
for(int j=from;j<=to;j++)
{
for(int k=from;k<=to;k++)
{
for(int g=from;g<=to;g++)
{
tmp=;
if(f[i-][j][k][g]>tmp)tmp=f[i-][j][k][g];
if(f[i-][j-][k][g]>tmp)tmp=f[i-][j-][k][g];
if(f[i-][j][k-][g]>tmp)tmp=f[i-][j][k-][g];
if(f[i-][j][k][g-]>tmp)tmp=f[i-][j][k][g-];
if(f[i-][j-][k-][g]>tmp)tmp=f[i-][j-][k-][g];
if(f[i-][j-][k][g-]>tmp)tmp=f[i-][j-][k][g-];
if(f[i-][j][k-][g-]>tmp)tmp=f[i-][j][k-][g-];
if(f[i-][j-][k-][g-]>tmp)tmp=f[i-][j-][k-][g-];
f[i][j][k][g]=tmp+a[j][i-j+];
if(j!=k)f[i][j][k][g]+=a[k][i-k+];
if(g!=k && g!=j)f[i][j][k][g]+=a[g][i-g+];
}
}
}
}
printf("%d",f[n+n-][n][n][n]);
return ;
}

T2:建造栅栏:

题目描述

勤奋的Farmer John想要建造一个四面的栅栏来关住牛们。他有一块长为n(4<=n<=2500)的木板,他想把这块本板切成4块。这四块小木板可以是任 何一个长度只要Farmer John能够把它们围成一个合理的四边形。他能够切出多少种不同的合理方案。注意: *只要大木板的切割点不同就当成是不同的方案(像全排列那样),不要担心另外的特殊情况,go ahead。 *栅栏的面积要大于0. *输出保证答案在longint范围内。 *整块木板都要用完。

输入

*第一行:一个数n

输出

*第一行:合理的方案总数

样例输入

6

样例输出

6

提示

Farmer John能够切出所有的情况为: (1, 1, 1,3); (1, 1, 2, 2); (1, 1, 3, 1); (1, 2, 1, 2); (1, 2, 2, 1); (1, 3,1, 1);

(2, 1, 1, 2); (2, 1, 2, 1); (2, 2, 1, 1); or (3, 1, 1, 1).

下面四种 -- (1, 1, 1, 3), (1, 1, 3, 1), (1, 3, 1, 1), and (3,1, 1, 1) – 不能够组成一个四边形.

题解:

F[i][j]表示把i分成j份的方案数,要保证合法,就得保证所有的方案中不包括>n/2的边

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll f[][];
int main()
{
int n;
cin>>n;
int from,to,k,pp;
if(n%)k=(n>>);
else k=(n>>)-;
f[][]=;
for(int i=;i<=n;i++)
{
from=i<?i:;
for(int j=;j<=from;j++)
{
to=j-<i-k?i-k:j-;
for(int g=to;g<=i-;g++)
f[i][j]+=f[g][j-];
}
}
printf("%lld",f[n][]);
return ;
}

T3:

题目描述

Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shown below

:

If some adjacent boxes are all of the same color, and both the box to
its left(if it exists) and its right(if it exists) are of some other
color, we call it a 'box segment'. There are 4 box segments. That is:
gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments
respectively.

Every time, you can click a box, then the whole segment containing
that box DISAPPEARS. If that segment is composed of k boxes, you will
get k*k points. for example, if you click on a silver box, the silver
segment disappears, you got 4*4=16 points.

Now let's look at the picture below:

N 个不同颜色的方块,排成一行,左右相邻颜色相同的方块属于同一段。上图有 4 段长度分
别为 1,4,3,1。
每次单击一个方块,包含这个方块的整段将消失。如果该段由 k 个方块组成,则将得到 k* k
分 。 例 如 , 如 果 你 点 击 一 个 银 盒 , 银 色 段 消 失 , 你 有 4 * 4 = 16 分 。

The first one is OPTIMAL.

Find the highest score you can get, given an initial state of this game.

输入

Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.

输出

For each test case, print the case number and the highest possible score.

样例输入

9 1 2 2 2 2 3 3 3 1

样例输出

29
 
题解:F[i][j][k]表示把i到j和后面和第一段颜色相同的 长度为k的一段合并的最大的得分

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int a[N],ls[N],col[N],f[N][N][N];
int dfs(int l,int r,int len)
{
if(l>r)return ;
if(f[l][r][len]!=-)return f[l][r][len];
f[l][r][len]=dfs(l,r-,)+(ls[r]+len)*(ls[r]+len);
for(int i=l;i<r;i++)
if(col[i]==col[r])f[l][r][len]=max(f[l][r][len],dfs(l,i,len+ls[r])+dfs(i+,r-,));
return f[l][r][len];
}
int main()
{
//freopen("pp.in","r",stdin);
int n,to=,m=;
scanf("%d",&n);
memset(f,-,sizeof(f));
a[n+]=-;
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
if(a[i]==a[i-])continue;
to=i;col[++m]=a[i];ls[m]=;
while(a[i]==a[to+])to++,ls[m]++;
}
printf("%d",dfs(,m,));
}

T4:[USACO15JAN]电影移动Moovie Mooving

题目描述

Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer John for L (1 <= L <= 100,000,000) minutes, during which time she wants to watch movies continuously. She has N (1 <= N <= 20) movies to choose from, each of which has a certain duration and a set of showtimes during the day. Bessie may enter and exit a movie at any time during one if its showtimes, but she does not want to ever visit the same movie twice, and she cannot switch to another showtime of the same movie that overlaps the current showtime.

Help Bessie by determining if it is possible for her to achieve her goal of watching movies continuously from time 0 through time L. If it is, determine the minimum number of movies she needs to see to achieve this goal (Bessie gets confused with plot lines if she watches too many movies).

奶牛贝西想连续看L (1 <= L <= 100,000,000)分钟的电影,有 N (1 <= N <= 20)部电影可供选择,每部电影会在一天的不同时段放映。

贝西可以在一部电影播放过程中的任何时间进入或退出放映厅。但她不愿意重复看到一部电影,所以每部电影她最多看到一次。她也不能在看一部电影的过程中,换到另一个正在播放相同电影的放映厅。

请帮贝西计算她能够做到从0到L分钟连续不断地观看电影,如果能,请计算她最少看几部电影就行了。

输入

The first line of input contains N and L.

The next N lines each describe a movie. They begin with its integer duration, D (1 <= D <= L) and the number of showtimes, C (1 <= C <= 1000). The remaining C integers on the same line are each in the range 0..L, and give the starting time of one of the showings of the movie. Showtimes are distinct, in the range 0..L, and given in increasing order.

输出

A single integer indicating the minimum number of movies that Bessie

needs to see to achieve her goal. If this is impossible output -1

instead.

样例输入

4 100 50 3 15 30 55 40 2 0 65 30 2 20 90 20 1 0

样例输出

3

提示

Bessie should attend the first showing of the fourth movie from time 0 to time 20. Then she watches the first showing of the first movie

from time 20 to time 65. Finally she watches the last showing of the second movie from time 65 to time 100.

题解:

水的状压DP,F[i]表示i状态下最大能消耗的时间 二分优化下就过了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-,ch=getchar();
return str;
}
int f[<<N],t[N],c[N],d[N][];
int pf(int i,int x)
{
int l=,r=c[i],mid,ans=;
while(l<=r)
{
mid=(l+r)>>;
if(d[i][mid]<=x)ans=d[i][mid],l=mid+;
else r=mid-;
}
return ans;
}
int main()
{
//freopen("pp.in","r",stdin);
int n=gi(),l=gi();
for(int i=;i<=n;i++)
{
t[i]=gi();c[i]=gi();
for(int j=;j<=c[i];j++)d[i][j]=gi();
}
int to,tmp;
for(int j=;j<=(<<n)-;j++)
{
for(int i=;i<=n;i++)
{
if(((<<(i-))&j))continue;
to=(j|(<<(i-)));
tmp=pf(i,f[j]);
if(tmp+t[i]>f[to])f[to]=tmp+t[i];
}
}
int ans=,sum=;
for(int j=(<<n)-;j>=;j--)
{
if(f[j]>l)
{
sum=;
tmp=j;
while(tmp>)tmp-=(tmp&(-tmp)),sum++;
if(sum<ans)ans=sum;
}
}
if(ans!=)
printf("%d",ans);
else printf("-1");
return ;
}

DP测试总结的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. Beta冲刺 第五天

    Beta冲刺 第五天 1. 昨天的困难 1.昨天的困难主要是在类的整理上,一些逻辑理不清,也有一些类写的太绝对了,扩展性就不那么好了,所以,昨天的困难就是在重构上. 页面结构太凌乱,之前没有统筹好具体 ...

  2. SQLAlchemy 教程 —— 基础入门篇

    SQLAlchemy 教程 -- 基础入门篇 一.课程简介 1.1 实验内容 本课程带领大家使用 SQLAlchemy 连接 MySQL 数据库,创建一个博客应用所需要的数据表,并介绍了使用 SQLA ...

  3. android 广播,manifest.xml注册,代码编写

    1.种 private void downloadBr(File file) {   // 广播出去,由广播接收器来处理下载完成的文件   Intent sendIntent = new Intent ...

  4. TOTP算法 基于时间的一次性密码

    /** Copyright (c) 2011 IETF Trust and the persons identified as authors of the code. All rights rese ...

  5. Hibernate之深入Hibernate的映射文件

    这周周末 要把hibernate的映射文件搞定 .. 1.映射文件的主结构 主要结构  :根元素为<hibernate-mapping ></hibernate-mapping> ...

  6. 完美解决某法院HP EVA8400删除VDISK问题

    [故障描述] 某地法院一台HP EVA8400存储,2组扩展柜,物理磁盘由12个1T FATA磁盘(AG691A 454414-001)和10个300G 15K FC磁盘(AG690A 454411- ...

  7. nyoj n-1位数

    n-1位数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的 ...

  8. 微信浏览器的页面在PC端访问

    微信浏览器的页面在PC端访问: 普通的在微信浏览器看的页面如果不在php代码中解析一下,然后复制链接在PC打开就出现无法访问,因为它复制的地址是: https://open.weixin.qq.com ...

  9. 我的PCB电路设计(一)

    我的制板规则 过孔大小:14/24mil-(12/22-28/50)  一般过孔没必要太大.如果电流较大可以适当增大过孔,或者多加几个过孔 线宽大小:小信号线8mil,大电流线不等按1A电流30mil ...

  10. DDD实战进阶第一波(二):开发一般业务的大健康行业直销系统(搭建支持DDD的轻量级框架一)

    要实现软件设计.软件开发在一个统一的思想.统一的节奏下进行,就应该有一个轻量级的框架对开发过程与代码编写做一定的约束. 虽然DDD是一个软件开发的方法,而不是具体的技术或框架,但拥有一个轻量级的框架仍 ...