poj-3056 http://poj.org/problem?id=3056
http://poj.org/problem?id=3056
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 995 | Accepted: 359 |
Description

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
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
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
题意:
题意:有偶数个人,所有人都必须互相敬酒,而且不能交叉,问在这种情况下,互相敬酒的人牌子相同的最大对数
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的更多相关文章
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- 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 ...
- POJ3278http://poj.org/problem?id=3278
http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- poj 1681 Painter's Problem(高斯消元)
id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...
- <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展 若满足要求则从左缩减区域 代码如下 正确性调整了几次 然后 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
随机推荐
- Ubuntu环境下手动配置HBase0.94.25
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- hdu 1309 Loansome Car Buyer
纯粹的阅读理解题………… ;}
- 【转】Windows平台SSH登录Linux并使用图形化界面
备注:经验证本文提供的方法可行且比使用VNC简洁一些.略有修改. [日期:2011-09-06] 来源:Linux社区 作者:tianhuadihuo http://www.linuxidc ...
- SpringMVC学习总结(三)——Controller接口详解(1)
4.12.ParameterizableViewController 参数化视图控制器,不进行功能处理(即静态视图),根据参数的逻辑视图名直接选择需要展示的视图. <bean name=&quo ...
- [Unity菜鸟] Final IK
由于本人英文较烂,边翻译用户手册边学习. 用户手册 IK Components Final IK 包含许多强大高速的IK组件 Aim AimIK solver是一个对CCD算法(cyclic co ...
- PowerDesigner连接Oracle数据库建表序列号实现自动增长
原文:PowerDesigner连接Oracle数据库建表序列号实现自动增长 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties — ...
- HTTP响应消息code解释
常见HTTP状态(304,200等) 在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Googl ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...
- JS代码片段:判断一个元素是否进入可视区域
// Determine if an element is in the visible viewport function isInViewport(element) { var rect = el ...
- NFC(3)Android上的NFC,开启NFC,3种NDEF数据
Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...