http://poj.org/problem?id=3056

The Bavarian Beer Party
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 995   Accepted: 359

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
题意:
题意:有偶数个人,所有人都必须互相敬酒,而且不能交叉,问在这种情况下,互相敬酒的人牌子相同的最大对数
d[i][j]表示第i个数到第j个数最大配对数
状态转移方程:

if(a[i]==a[j])
                    dp[i][j]=dp[i+1][j-1]+1;
                else
                    dp[i][j]=dp[i+1][j-1];d[i][j]=max(d[i][j],d[i][k]+d[k+1][j])          (i<=k<j)

主要注意的是:要解决不能交叉,而且每个教授都要敬酒,所以两头尾之间相隔的的人数必须为0,2,4..等偶数。

怎样控制区间之间是偶数呢?
for(k=1;k<=m;k+=2)
如果for(k=0;k<m;k+=2),那么就会控制区间之间为奇数。
试试就知道了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[][];
int main()
{
int i,j,k,m,t,a[],g;
cin>>t;
while(t--)
{
memset(dp,,sizeof(dp));
cin>>m;
for(i=;i<m;i++)
cin>>a[i];
for(k=;k<=m;k+=)//因为不能交叉,
//所以两头尾之间相隔的的人数必须为0,2,4..等偶数
//如果for(k=0;k<m;k+=2),那么就会控制区间之间为奇数。
{
for(i=;i<m-k;i++)
{
j=i+k;
if(a[i]==a[j])
dp[i][j]=dp[i+][j-]+;
else
dp[i][j]=dp[i+][j-];
for(g=i;g<j;g++)
dp[i][j]=max(dp[i][j],dp[i][g]+dp[g+][j]);//分割区间求最优。 }
}
cout<<dp[][m-]<<endl;
}
return ;
}


poj-3056 http://poj.org/problem?id=3056的更多相关文章

  1. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. POJ3278http://poj.org/problem?id=3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  4. OpenJudge/Poj 1207 The 3n + 1 problem

    1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...

  5. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. poj 1681 Painter&#39;s Problem(高斯消元)

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...

  7. POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!

    水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...

  8. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  9. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

随机推荐

  1. Biba模型简介

    上周上信息安全的课,老师留了个Biba模型的作业.自己看书了解了一下,记录如下. 参考资料:石文昌<信息系统安全概论第2版> ISBN:978-7-121-22143-9 Biba模型是毕 ...

  2. [转]Ubuntu 常用快捷键10个

    转自:http://www.linuxeden.com/html/news/20100613/103374.html 1.前一个后一个工作区的切换 如果你经常使用工作区,那你就可以用Ctrl + Al ...

  3. 解决virtualbox 虚拟机不能ping通win7

    凭经验猜测是由于防火墙引起的,关闭防火墙再ping,果然可行.google说这是由于“win7 防火墙默认的禁ping策略”引起的.但是关闭防火墙很不安全,可以按照以下步骤为防火墙添加入站规则来解决问 ...

  4. 关于JS中变量的作用域-实例

    先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...

  5. python url编码,解码

    >>> urllib.unquote('%E4%B8%BD%E6%B1%9F') >>> data '\xe4\xb8\xbd\xe6\xb1\x9f' >& ...

  6. vss的ss.ini丢失或损坏导致的vss无法登录错误

    vss的ss.ini丢失或损坏导致的vss无法登录错误 Written in 2007-07-03 18:17 在vss使用过程中,不知道什么原因,会导至vss目录中的ss.ini文件损坏,此文件位于 ...

  7. context:component-scan扫描使用上的容易忽略的use-default-filters

    问题 如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean.正确 <context:component-scan base-pa ...

  8. Spring使用外部的配置文件

    在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据 ...

  9. 控制CPU占用率曲线

    编程之美的第一个问题,我的机器是双核的,用文中的代码,得到的曲线波动比较大额,受其他进程的影响比较大.文中提到10ms接近windows的调度时间片,如果选得太小,会造成线程被频繁唤醒和挂起,造成内核 ...

  10. python处理Excel

    Python中一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库复制和修改Excel文件.这三个库只支持到Excel2003. python-excel主 ...