POJ3056:The Bavarian Beer Party(区间DP)
Description

Figure 2: Toasting across a table with eight persons:no arms crossing(left), arms crossing(right)
We know that the professors like to toast with someone that is drinking the same brand of beer, and we like to maximize the number of pairs of professors toasting with the same brand , again without crossing arms. Write an algorithm to do this, keeping in mind that every professor should take part in the toasting.
Input
One line with an even number p, satisfying 2 <= p <= 1000: the number of participants
One line with p integers (separated by single spaces) indicating the beer brands fro the consecutive professors( in clockwise order, starting at an arbitrary position). Each value is between 1 and 100 (boudaries included).
Output
Sample Input
2
6
1 2 2 1 3 3
22
1 7 1 2 4 2 4 9 1 1 9 4 5 9 4 5 6 9 2 1 2 9
Sample Output
3
6
题意:有偶数个人,所有人都必须互相敬酒,而且不能交叉,问在这种情况下,互相敬酒的人牌子相同的最大对数
思路:又是一道比较简单的区间DP,要注意的是,所有人都必须敬酒,而且不能交叉,这种情况分区间解决即可
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t,n,a[1005],dp[1005][1005],i,j,k,l; int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(l = 1; l<n; l+=2)//l代表是一个区间内头与尾的间距,因为不能交叉,所以两头尾之间相隔的的人数必须为0,2,4..等偶数
{
for(i = 1; i+l<=n; i++)
{
j = i+l;
dp[i][j] = dp[i+1][j-1];
if(a[i] == a[j])//头尾相等,对数加1
dp[i][j]++;
for(k = i+1; k<j; k+=2)//分割的区间也是同样道理
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
}
}
printf("%d\n",dp[1][n]);
} return 0;
}
POJ3056:The Bavarian Beer Party(区间DP)的更多相关文章
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Java [leetcode 25]Reverse Nodes in k-Group
题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- openstack学习线路指导
原文链接: http://www.aboutyun.com/thread-7225-1-1.html 网上很多hadoop资料,openstack资料相对较少,这里整理一下,帮助初学者尽快入门. 首先 ...
- openstack api users list get token get servers
curl -i \ -H "Content-Type: application/json" \ -d '{ "auth": { "ident ...
- Android实例-操作sqlite数据库之Grid显示图片(XE8+小米2)
结果: 1.数据库文件,记得打包到程序中(assets\internal\). 操作方法: 1.新建firemonkey mobile application①菜单->File->New- ...
- 【C语言】- 数据输出-printf( )和putchar( )
格式化输出函数printf( ) printf( )功能: 向系统指定输出设备按指定的格式输入任意个任意类型的数据,并返回实际输出的字符数.若出错,将返回负数. printf( )使用形式: prin ...
- [iOS基础控件 - 6.3] 使用可视化连线方式指定dataSource、delegate
对着要指定dataSource或者delegate的控件右击,然后拖动线到指定的控制器上
- Jquery ajax 得到返回值
Jquery ajax 得到返回值 1.ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false,//同步. 2.必须定义一个全局变量 var result = ...
- configparser
configparser configparser模块是python中用来处理类似于windows的ini格式文件, 一个ini文件的格式 [section] key = value
- BootStrap最常用的几个插件(V3.3.0版)
1.标签页 <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> < ...