POJ3056:The Bavarian Beer Party(区间DP)
来源:互联网 发布:淘宝买家贷款操作流程 编辑:程序博客网 时间:2024/06/07 02:23
Description
The professors of the Bayerische Mathematiker Verein have their annual party in the local Biergarten. They are sitting at a round table each with his own pint of beer. As a ceremony each professor raises his pint and toasts one of the other guests in such a way that no arms cross.
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.
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
The frist line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
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).
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
For every test case in the input, the output should contain a single number on a single line: the maximum number of non-intersecting toasts of the same beer brand for this test case.
Sample Input
261 2 2 1 3 3221 7 1 2 4 2 4 9 1 1 9 4 5 9 4 5 6 9 2 1 2 9
Sample Output
36
题意:有偶数个人,所有人都必须互相敬酒,而且不能交叉,问在这种情况下,互相敬酒的人牌子相同的最大对数
思路:又是一道比较简单的区间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)
- POJ3056:The Bavarian Beer Party(区间DP)
- poj 3056 The Bavarian Beer Party (区间dp)
- poj 3056 The Bavarian Beer Party (区间DP)
- POJ 3056 The Bavarian Beer Party (区间DP)
- poj3056The Bavarian Beer Party(区间dp)
- 【poj 3056】The Bavarian Beer Party (区间DP+最大匹配不交叉的简单算法)
- poj3056The Bavarian Beer Party (不交差配对,区间DP)
- POJ 3056 The Bavarian Beer Party 笔记
- POJ3056The Bavarian Beer Party题解动态规划DP
- HDU 3506 Monkey Party(区间DP)
- hdu3506 Monkey Party--区间dp & 石子归并 & dp四边形优化
- HDU 3506 Monkey Party 区间dp+四边形不等式
- nyoj749Splits the string(区间dp)
- hdu4283 You Are the One(区间DP)
- HDU4283:You Are the One(区间DP)
- poj3042 Grazing on the Run[区间dp]
- POJ3186:Treats for the Cows(区间DP)
- 位运算 进制转换
- JavaBean
- Android知识点textview加横线的属性
- 黑马程序员---面向对象学习
- poj题目分类
- POJ3056:The Bavarian Beer Party(区间DP)
- JavaSE基础之Java语言的跨平台原理(JVM)
- UE 替换
- 数字图像处理:13.MATLAB程序实现
- Linux下对date和timestamp的互转
- Web 开发
- poj2378 树状dp
- 黑马程序员:Java基础篇之Java概述+语法
- 黑马程序员---static的用法