ccc 16 s4 Combining Riceballs

原创 2016年08月30日 22:52:51

Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possible for his friend to eat. Alphonse can perform the following operations:

• If two adjacent rice balls have the same size, Alphonse can combine them to make a new rice ball. The new rice ball’s size is the sum of the two old rice balls’ sizes. It occupies the position in the row previously occupied by the two old rice balls.

• If two rice balls have the same size, and there is exactly one rice ball between them,Alphonse can combine all three rice balls to make a new rice ball. (The middle rice ball does not need to have the same size as the other two.) The new rice ball’s size is the sum of the three old rice balls’ sizes. It occupies the position in the row previously occupied by the three old rice balls.

Alphonse can perform each operation as many times as he wants.Determine the size of the largest rice ball in the row after performing 0 or more operations.


Input Specification

The first line will contain the integer, N (1 ≤ N ≤ 400).The next line will contain N space separated integers representing the sizes of the rice balls, in order from left to right. Each integer is at least 1 and at most 1 000 000.

Output Specification

Output the size of the largest riceball Alphonse can form.


Sample Input 1

7 4 7 12 12 3 9 9 3

Output for Sample Input

48

Explanation for Output for Sample Input 1

One possible set of moves to create a riceball of size 48 is to combine 12 and 12, forming a riceball of size 24. Then, combine 9 and 9 to form a riceball of size 18. Then, combine 3, 18 and 3 to forma riceball of size 24. Finally, combine the two riceballs of size 24 to form a riceball of size 48.


Sample Input 2

4

1 2 3 1

Output for Sample Input 2

3

Explanation for Output for Sample Input 2There are no moves to make, thus the largest riceball in the row is size 3


Solution: dynamic programming

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 400 + 5;
int n; int a[maxn]; int s[maxn];

void init() {
	scanf("%d", &n);
	for (int i = 0 ; i < n; i++) scanf("%d", &a[i]);
	s[0] = a[0];
	for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
}

int sum(int i, int j) {
	return s[j] - s[i] + a[i];
}

bool f[maxn][maxn];

void solve() {
	int ans = 0;
	memset(f, false, sizeof(f));
	for (int i = 0; i < n; i++) f[i][i] = true;
	
	for (int len = 1; len <= n; len ++ ) {
		for (int i = 0; i + len - 1 < n; i ++ ) {
			int j = i + len - 1;
			
			if (f[i][j]) { ans = max(ans, sum(i, j)); continue; }
			
			for (int k = i; k < j; k ++ ) {
				if (f[i][k] && f[k + 1][j] && sum(i, k) == sum(k + 1, j)) { f[i][j] = true; break; }
			}
			
			if (f[i][j]) { ans = max(ans, sum(i, j)); continue; }
			
			for (int len2 = 1; len2 <= len - 2; len2 ++ ) {
				if (f[i][j]) break;
				for (int k = i + 1; k + len2 <= j; k ++ ) {
					int t = k + len2 - 1;
					
					if (f[i][k - 1] && f[k][t] && f[t + 1][j] && sum(i, k - 1) == sum(t + 1, j)) {
						f[i][j] = true; break;
					}
				}
			}
			
			if (f[i][j]) ans = max(ans, sum(i, j));
		}
	}

	printf("%d\n", ans);
}

int main() {
	init(); solve();
	return 0;
}

相关文章推荐

修正出货单确认时计算客户信用度(s_ccc.4gl)_订单未转出货金额BUG

如客户

iPhone4S水货到沪16G7500元起 行货年底进内地

苹果iPhone4S水货在沪开售,16G、32G版价格分别在7500元和万元左右,这让不少消费者望而却步。另有消息称,iPhone4s行货将于年底进入内地,电信、联通目前都在争取成为运营商。水货价格7...

Microbiome:HiSeq平台16S扩增子超高通量测序文库构建方法

Microbiome:HiSeq平台16S扩增子超高通量测序文库构建方法摘要背景先进的测序技术和生物信息分析,使微生物群体分析的技术路线非常成熟。然而,在数据产生过程中的技术仍需改进,如增加通量并降低...

STC15W4K16S4单片机的OLED驱动程序V1

  • 2017年11月05日 18:29
  • 13KB
  • 下载

S5PV210--1---210启动方式和代码前16字节

按照三星《S5PV210_UM_REV1.1》手册上说明的启动流程为:S5PV210上电将从IROM(interal ROM)处执行固化的启动代码,它对时钟等初始化、对启动设备进行判断,并从启动设备中...

不停产工业级SDRAM-AS4C16M16S-6TIN/AS4C16M16SA-6TIN 美国ALLIANCE代理商

Alliance亚太区代理商 大盛唐电子集团有限公司 代理ALLIANCE全线产品:SRAM,SDRAM,DDR1,DDR2,DDR3,Mobile DDR等 TEL:021-604929...
  • szdst
  • szdst
  • 2016年04月11日 21:45
  • 799

站群服务器用SS5 SOCKS5 配置多IP/多端口高性能代理服务器(16G内存主机支持超过100万并发S5代理)

SS5 是高性能的 SOCKS 代理服务器,支持 SOCK4 和 SOCKS5 协议。下面的安装指南详细描述了如何在 CentOS 6.x服务器上安装和配置SS5,运行多个 ss5 进程来绑定不同端口...
  • lic95
  • lic95
  • 2015年09月12日 22:36
  • 7089
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ccc 16 s4 Combining Riceballs
举报原因:
原因补充:

(最多只允许输入30个字)