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. REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口

    哈哈,终于快结束了.. urls.py from django.conf.urls import include, url from django.contrib import admin urlpa ...

  2. Ubuntu 装JDK

    我是按照这篇文章安装jdk的: http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html   先去 Oracle下载Linux下 ...

  3. 两个List合并,过滤重复记录

    import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...

  4. sql语句面试总结

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name   kecheng   fenshu 张三     语文       81张三     数学       75李四     语文   ...

  5. Swift 版本很好的卡片切换效果基于ZLSwipeableView

    前言:在这篇文章你可以学到,一些基本的Swift语法, 基本UI控件闭包等. 实际的效果,比gif图的效果好很多. 卡片切换.gif 首先需要导入ZLSwipeableView pod 'ZLSwip ...

  6. navigationbar的一些设置记录

    1.设置navigationbar背景颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; PS:如果颜色不对 ...

  7. [Codeforces677B]Vanya and Food Processor(模拟,数学)

    题目链接:http://codeforces.com/contest/677/problem/B 题意:n个土豆,每个土豆高ai.现在有个加工机,最高能放h,每次能加工k.问需要多少次才能把土豆全加工 ...

  8. gulp自动刷新插件

    gulp自动刷新的插件很多,但是感觉最好用的还是 browser-sync 插件.如果不想用命令行,也可以使用 browser-sync界面工具 先安装 browser-sync 插件: npm in ...

  9. npm安装插件提示

    现在使用node的人群越来越多,咱也不能落后,得跟紧脚步才行. 今天使用 npm 安装插件的时候,出现以下提示,而且,安装程序看着好像终止了.而且,看到一个exit单词,就认为说安装出错了. $ np ...

  10. hdu 4825 Xor Sum (建树) 2014年百度之星程序设计大赛 - 资格赛 1003

    题目 题意:给n个数,m次询问,每次给一个数,求这n个数里与这个数 异或 最大的数. 思路:建一个类似字典数的数,把每一个数用 32位的0或者1 表示,查找从高位向底位找,优先找不同的,如果没有不同的 ...