# 中国海洋大学第四届朗讯杯高级组 The Urge to Merge

## 题目描述

The Acme Consulting Group has sent you into a new technology park to enhance dynamism, synergy and sustainability. You\'re not sure what any of these terms mean, but you\'re pretty good at making money, which is what you plan on doing. The park consists of a 3 × n grid of facilities. Each facility houses a start-up with an inherent value. By facilitating mergers between neighboring start-ups, you intend to increase their value, thereby allowing you to fulfill your life-long dream of opening your own chain of latte-and-burrito shops.

Due to anti-trust laws, any individual merger may only involve two start-ups and no start-up may be involved in more than one merger. Furthermore, two start-ups may only merge if they are housed in adjacent facilities (diagonal doesn\'t count). The added value generated by a merger is equal to the product of the values of the two start-ups involved. You may opt to not involve a given start-up in any merger, in which case no added value is generated. Your goal is to find a set of mergers with the largest total added value generated. For example, the startup values shown in the figure on the left, could be optimally merged as shown in the figure on the right for a total added value of 171.

## 输入

The first line of each test case will contain a single positive integer n 1000 indicating the width of the facilities grid. This is followed by three lines, each containing n positive integers (all  100) representing the values of each start-up. A line containing a single 0 will terminate input.

## 输出

For each test case, output the maximum added value attainable via mergers for that set of start-ups.

## 示例输入

4
7  2  4  9
3  5  9  3
9  5  1  8
0

## 示例输出

Case 1: 171

## 来源

中国海洋大学第四届朗讯杯高级组

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1005][15];
int grid[5][1005];
int n;
int Convers(int v,int a)
{
switch(v)
{
case 1:
return grid[1][a-1]*grid[1][a];
case 2:
return grid[2][a-1]*grid[2][a];
case 3:
return grid[3][a-1]*grid[3][a];
case 4:
return grid[2][a-1]*grid[2][a]+grid[3][a-1]*grid[3][a];
case 5:
return grid[1][a-1]*grid[1][a]+grid[3][a-1]*grid[3][a];
case 6:
return grid[1][a-1]*grid[1][a]+grid[2][a-1]*grid[2][a];
case 7:
return grid[1][a-1]*grid[1][a]+grid[2][a-1]*grid[2][a]+grid[3][a-1]*grid[3][a];
case 8:
return grid[1][a]*grid[2][a];
case 9:
return grid[2][a]*grid[3][a];
case 10:
return grid[1][a-1]*grid[1][a]+ grid[2][a]*grid[3][a];
case 11:
return grid[3][a-1]*grid[3][a]+ grid[1][a]*grid[2][a];
}
}
int main()
{
int kase=0;
while(scanf("%d",&n)==1&&n)
{
memset(grid,0,sizeof(grid));
for(int i=1; i<=3; ++i)
for(int j=1; j<=n; ++j)
scanf("%d",&grid[i][j]);
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=11; ++j)
{
int res=0;
if(i>1)
{
switch(j)
{
case 1:
res=max(res,max(dp[i-1][2],max(dp[i-1][3],max(dp[i-1][4],dp[i-1][9]))));
break;
case 2:
for(int k=1; k<=11; ++k)
res=max(res,dp[i-2][k]);
res=max(res,max(dp[i-1][1],max(dp[i-1][3],dp[i-1][5])));
break;
case 3:
res=max(res,max(dp[i-1][1],max(dp[i-1][2],max(dp[i-1][6],dp[i-1][8]))));
break;
case 4:
res=max(res,dp[i-1][1]);
for(int k=1; k<=11; ++k)
res=max(res,dp[i-2][k]);
break;
case 5:
res=max(res,dp[i-1][2]);
for(int k=1; k<=11; ++k)
res=max(res,dp[i-2][k]);
break;
case 6:
res=max(res,dp[i-1][3]);
for(int k=1; k<=11; ++k)
res=max(res,dp[i-2][k]);
break;
case 7:
for(int k=1; k<=11; ++k)
res=max(res,dp[i-2][k]);
break;
case 8:
for(int k=1; k<=11; ++k)
res=max(res,dp[i-1][k]);
break;
case 9:
for(int k=1; k<=11; ++k)
res=max(res,dp[i-1][k]);
break;
case 10:
res=max(res,max(dp[i-1][2],max(dp[i-1][3],max(dp[i-1][4],dp[i-1][9]))));
break;
case 11:
res=max(res,max(dp[i-1][1],max(dp[i-1][2],max(dp[i-1][6],dp[i-1][8]))));
break;
}
}
if(i==1)
{
if(j==8||j==9)
dp[i][j]=Convers(j,i);
else
dp[i][j]=0;
}
else
dp[i][j]=res+Convers(j,i);
}
}
int ans=0;
for(int i=1; i<=11; ++i)
ans=max(dp[n][i],ans);
printf("Case %d: ",++kase);
printf("%d\n",ans);
}
return 0;
}

#### SDUTOJ（2504）多项式求和

2018-02-26 22:35:08

#### 中国海洋大学第四届朗讯杯高级组 Playing Fair with Cryptography

2013-12-01 15:01:02

#### 中国海洋大学第四届朗讯杯高级组 Cash Cow

2013-12-01 12:50:00

#### 中国海洋大学第四届朗讯杯高级组 Super Phyllis

2013-12-02 16:10:47

#### 浪潮杯

2017-06-12 21:13:56

#### 中国海洋大学910高级程序设计回忆.docx

2017年11月17日 14KB 下载

#### 中国海洋大学各个校区都有什么专业

2017-12-19 14:55:25

#### 2012年中国海洋大学考研真题910高级程序设计

2013年11月14日 9.98MB 下载

#### 基于MATLAB的电商平台图片中文字的提取与识别（第四届泰迪杯全国数据挖掘竞赛A题）

2016-11-27 12:47:22