ccc 2016 s4 Combining Riceballs

原创 2016年08月30日 23:07:21

Problem Description

Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possiblefor 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 newrice ball. The new rice ball’s size is the sum of the two old rice balls’ sizes. It occupies theposition 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 balldoes not need to have the same size as the other two.) The new rice ball’s size is the sum ofthe 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.


#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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ccc 16 s4 Combining Riceballs

Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possible...

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

如客户

Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) CF618A A. Slime Combining

A. Slime Combining time limit per test2 seconds memory limit per test256 megabytes inputstandard ...

【Wunder Fund Round 2016 (Div 1 + Div 2 combined)A】【二进制拆分】Slime Combining 特殊规则二进制拆分

A. Slime Combining time limit per test 2 seconds memory limit per test 256 megabytes ...

Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)--A. Slime Combining

A. Slime Combining time limit per test 2 seconds memory limit per test 256 megabytes input sta...

金盾高级视频加密系统2016S

  • 2014-08-29 16:16
  • 27.15MB
  • 下载

【HDU5894 2016 ACM ICPC Asia Regional Shenyang Online C】【组合数 隔板法】hannnnah_j’s Biological Test n个不同座位成

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

【2016杭电女生赛1009】【挖掘本质找关系 模拟】sum 找到最小的正整数,满足a乘sum(n)==b乘S(2n)

#include #include #include #include #include #include #include #include #include #include #include #...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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