poj3056The Bavarian Beer Party (不交差配对,区间DP)
来源:互联网 发布:正保软件 编辑:程序博客网 时间:2024/05/21 22:55
Problem 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
题意:有n个人围着卓,所有人都要成对敬酒,且不能有交差的,求最多有多少对成对敬酒。
题解:要所有可能成对且不交差,那么区间内一定是偶数个人。
#include<stdio.h>#include<string.h>int dp[2005][2005];int max(int a,int b){ return a>b?a:b;}int main(){ int t,n,ans[2005]; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&ans[i]); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp[i][j]=0; for(int r=1;r<n;r+=2)//保证区间人数为偶数,只有是偶数才能所有人成对敬酒 for(int i=1;i<=n-r;i++) { int j=i+r; dp[i][j]=dp[i+1][j-1];//如果在区间内没有与i成对时 for(int k=i+1;k<=j;k+=2)//保持小区间也成偶数 if(ans[i]==ans[k]) dp[i][j]=max(dp[i][j],dp[i+1][k-1]+1+dp[k+1][j]); else dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]); } printf("%d\n",dp[1][n]); }}
0 0
- poj3056The Bavarian Beer Party (不交差配对,区间DP)
- poj3056The Bavarian Beer Party(区间dp)
- POJ3056The Bavarian Beer Party题解动态规划DP
- 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)
- POJ 3056 The Bavarian Beer Party (区间DP)
- POJ 3056 The Bavarian Beer Party 笔记
- HDU 3506 Monkey Party(区间DP)
- hdu3506 Monkey Party--区间dp & 石子归并 & dp四边形优化
- NYOJ15括号配对(二)(区间dp)
- POJ 2955 Brackets (区间DP 经典括号配对)
- HDU 3506 Monkey Party 区间dp+四边形不等式
- MM不哭【区间DP】
- 【HDU3506,HOJ2952】Monkey Party-环状区间合并DP+四边形不等式优化
- hdu 3506 Monkey Party (区间dp 求环形石子合并+四边形不等式优化)
- 在微博的世界里,娱乐圈的明星一直拥有绝对的号召力
- 信任、公平、梦想—新拍拍,新起点-拍拍网蒉莺春
- IOS使用Jenkins进行持续集成
- DML、DDL、DCL区别
- C程序存储空间布局
- poj3056The Bavarian Beer Party (不交差配对,区间DP)
- 重新开始的一页
- Advanced Installer汉化版教程(打包程序,安装包制作)
- 需要加载整文档和构造OpenEIM DOM树
- 一般常量和字符串常量在内存中的存储
- php学习之道:坑爹的file_exists
- C++智能指针详解
- php——学习笔记,检索字符串和它的次数,替换字符串
- 编程挑战。三角形问题nlogn的算法