跳到正文

个人记录

aaa

ttkxydyr约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月18日
收录
2026年10月18日
更新
2026年10月18日
篇幅
约 3 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 100;

int n, c[N];
ll ans, a[N];

inline ll solve()
{
	// 输出solve函数所求解的状态是什么
	// 输出所有的中间量来保证每个步骤的正确性 
//	printf("at solve:");
//	for(int i = 1; i <= n; i++) {
//		printf("%d", c[i]);
//	}
//	printf("\n");
	
	ll pre0 = -1, pre1 = -1, ret = 0;
	for(int i = 1; i <= n; i++) {
//		printf("	at %d, a=%d c=%d p0=%d p1=%d ret=%d\n", i, a[i], c[i], pre0, pre1, ret);
		if(c[i] == 0) {
//			printf("	in if c[i]=0");
			if(pre0 != -1) {
//				printf("ret from %d to %d\n", ret, ret+a[i] - pre0);
//				printf("	add %d-%d=%d to ret\n", a[i], pre0, a[i] - pre0);
				ret += a[i] - pre0;
			}
//			printf("pre0 from %d to %d\n", pre0, a[i]);
			int tmp = pre0;
			pre0 = a[i];
			
		}
		else { // c[i] == 1
			if(pre1 != -1) {
				ret += a[i] - pre1;
			}
			pre1 = a[i];
		}
	}
	return ret;
}

// 通过在搜索结果处做进入计数
// 检查是否搜索状态有缺漏 
//int debugcnt = 0;

inline void dfs(int dep)
{
	// 输出当前位置和前缀颜色
	// 目的是检查搜索是否齐全 
//	printf("dfs(%d): ", dep);
//	for(int i = 1; i < dep; i++) {
//		printf("%d ", c[i]);
//	}
//	printf("\n");
	
	if(dep == n+1) {
		// 输出当前颜色串和答案
		// 目的是检查问题是否出在solve函数
		// 以及检查结束部分的判定条件是否有误 
//		printf("dfs(%d): ", dep);
//		for(int i = 1; i < dep; i++) {
//			printf("%d ", c[i]);
//		}
//		printf("\n");
//		debugcnt++;
		ll ret = solve();
//		printf("ans from %d to %d\n", ans, max(ans, ret));
		ans = max(ans, ret);
//		printf("	at this point, ret=%d\n", ret);
		return;
	}
	c[dep] = 1; dfs(dep+1);
	c[dep] = 0; dfs(dep+1);
}

int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	ans = 0;
	dfs(1);
	cout << ans;
	
//	printf("%d\n", debugcnt);

	return 0;
}
原文