poj 3056 The Bavarian Beer Party (区间DP)

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.
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).
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

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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1005],dp[1005][1005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int k=1;k<n;k=k+2)//因为不能交叉,所以两头尾之间相隔的的人数必须为0,2,4..等偶数  
            for(int i=0,j=i+k;j<n;i++,j++)
            {
                dp[i][j]=dp[i+1][j-1];
                if(a[i]==a[j])
                    dp[i][j]++;
                for(int d=i;d<j;d++)
                    dp[i][j]=max(dp[i][j],dp[i][d]+dp[d+1][j]);
            }
        printf("%d\n",dp[0][n-1]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值