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 <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)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

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

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. RMAN备份与恢复实例

    1. 检查数据库模式:   sqlplus /nolog    conn /as sysdba   archive log list (查看数据库是否处于归档模式中) 若为非归档,则修改数据库归档模式 ...

  2. Android 自动换行流式布局的RadioGroup

    效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...

  3. (一)学习CSS之z-index属性

    参考:http://www.w3school.com.cn/cssref/pr_pos_z-index.asp z-index 属性设置元素的堆叠顺序.拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元 ...

  4. 浅谈reverse_iterator的base()函数

    非原创,原文链接:http://blog.csdn.net/shuchao/article/details/3705252 调用reverse_iterator的base成员函数可以产生"对 ...

  5. 609C Load Balancing

    题意: ”平均数“的意思是:最大数和最小数之间的差值为0或1: 先求“平均”数组,再相减. #include<iostream> #include<cstdlib> #incl ...

  6. HDU2015校赛 The Magic Tower

    题意:两行分别是W和B的生命值,攻击值,防御值. 如果W先,W的攻击值-B的防御值大于零则B生命值减去这么多,然后该B攻击.直到谁的生命值先小与等于零则攻击的人赢. 输出写错了..... 错误代码 # ...

  7. lcov收集覆盖率

    1.gcov 1.1 什么是gcov 首先我们要了解什么是gcov,gcov伴随gcc 发布.gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用 ...

  8. cocos2d-x的helloLua例子函数名定义误导初学者

    初次研究cocos2d-x, cocos2d-x支持lua是一个很不错的功能,使用lua来开发有个最大的好处就是不用每次改了游戏代码都编译,大多数情况下改了脚本直接运行程序就可以了,发布更新时也不用更 ...

  9. jetty上传 Form too large: 275782 > 200000

    1,找到jetty服务器下的jetty.xml,在 <Configure id="Server" class="org.eclipse.jetty.server.S ...

  10. Django ORM 中的批量操作

    Django ORM 中的批量操作 在Hibenate中,通过批量提交SQL操作,部分地实现了数据库的批量操作.但在Django的ORM中的批量操作却要完美得多,真是一个惊喜. 数据模型定义 首先,定 ...