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. PostgreSql字符串函数和操作符

    本节描述了用于检查和操作字符串数值的函数和操作符.在这个环境中的字符串包括所有 character, character varying, text 类型的值.除非另外说明,所有下面列出的函数都可以处 ...

  2. WCF学习笔记(一):WCF简介

    转:http://www.cnblogs.com/wengyuli/archive/2009/11/04/1595693.html MSDN上关于WCF给出如下注解: 设计 Windows Commu ...

  3. Linux中的设备文件与设备号

    设备文件与设备号   在Linux下,一切皆文件,设备也不例外,为了管理这些设备,系统为它们各自都编了号,而每个设备号又分为主设备号和次设备号.主设备号用来区分不同类型的设备,而次设备号用来区分同一类 ...

  4. 【译】 AWK教程指南 4通过文本内容和对比选择指定的记录

    Pattern { Action }为awk中最主要的语法.若某Pattern的值为真则执行它后面的 Action. awk中常使用"关系表达式" (Relational Expr ...

  5. oracle 启用归档日志

    Oracle可以运行在2种模式下:归档模式(archivelog)和非归档模式(noarchivelog) 归档模式可以提高Oracle数据库的可恢复性,生产数据库都应该运行在此模式下,归档模式应该和 ...

  6. HDU4614 Vases and Flowers

    http://acm.hdu.edu.cn/showproblem.php?pid=4614 HDU 4614 Vases and Flowers (2013多校第二场线段树) // #pragma ...

  7. KNN及其改进算法的python实现

    一. 马氏距离 我们熟悉的欧氏距离虽然很有用,但也有明显的缺点.它将样品的不同属性(即各指标或各变量)之间的差别等同看待,这一点有时不能满足实际要求.例如,在教育研究中,经常遇到对人的分析和判别,个体 ...

  8. 【noip2011】Mayan游戏

    题解: 刷了一天的noip啊 做了10题! 突然找回了做马拉松的感觉- - 我中午竟然放弃治疗去看视频 做到晚上累得都快挂了 用电脑放一些rock 把音乐当咖啡硬撑下来 但是还是没能刷3届 唉 显然速 ...

  9. nodejs学习:师哥自家的twenty博客框架

    这周继续为DTree项目预热,学习sails框架的搭建和结构熟悉.正好师哥在做一个nodejs的CMS框架twenty,他们用的就是sails框架. 结构 首先简单了解一下结构.在jade文件里由an ...

  10. 使用Intent在活动之间穿梭(《第一行代码》读书笔记)

    以下全是个人理解//瞎扯 其实活动理解理解起来就像一个个函数 那么Intent就是调用函数和参数传递 可以有无参,仅仅是调用 Intent intent = new Intent(A.this, B. ...