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> ...
随机推荐
- Android 父类super.onDestroy();的有关问题
super.onDestroy(); 的问题. 注意:没有显式地在自己的方法中调用父类Activity的onDestroy是会报错的.我的问题很简单,在我覆盖的onDestroy(),方法中需要调用父 ...
- FileOutputStream和FileInputStream
package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- QTableWidget嵌入QpushButton后定位是哪一个QpushButton
问题: 有时候会遇到这样的情况,在QTableWidget中我们需要嵌入一个QpushButton按钮,但是如何确定是哪个Button按下的呢? 解决: 一般地,一个按钮按下后会连接到一槽函数,那么在 ...
- C#操作.csv文件Demo
1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...
- JAVA+ Proxool+ SQLserver 2008 “signer information does not match signer information of other classes in the same package”
1. Proxool+SQLserver2008(sqljdbc4.jar)集成问题最近在项目中遇到个问题:我用的是Proxool连接池,连接SQLserver2008数据库,控制台报错:签名信息和同 ...
- Use Eclipse to develop groovy[docs.codehaus.org]
http://docs.codehaus.org/display/GROOVY/Install+Groovy-Eclipse+Plugin http://docs.codehaus.org/displ ...
- hive环境的搭建
hive 默认用的是derby数据库存储源数据,在这改为 mysql来存储: 1.hive和关系数据库的对照关系 hive 所要查询的数据保存在HDFS中: hive 中的数据库和表对应HDFS中的文 ...
- Support Library官方教程(1)概述
Support Library The Android Support Library package is a set of code libraries that provide backward ...
- cd /d %~dp0
cd /d %~dp0 注册服务的bat要用到,普通的执行没关系,但是用“管理员角色”执行,默认的起始目录会到 c:\windows\system 下,用上面一句可以回到当前执行bat的目录
- bzoj1832: [AHOI2008]聚会
写过的题... #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...