CF 1144 G Two Merged Sequences(贪心 or DP)

14 篇文章 0 订阅
1 篇文章 0 订阅

参考博客:https://www.luogu.org/problemnew/solution/CF1144G

思路:

用dp[i][0]表示ai在递增序列中时,递减序列最小元素(最后一个元素)的最大可能值。

用dp[i][1]表示ai在递减序列中时,递增序列最大元素(最后一个元素)的最小可能值。

转移的时候就是:

dp[i][0]可能由a[i-1]和dp[i-1][0]的较大值

dp[i][1]可能由a[i-1]和dp[i-1][1]的较小值

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
	
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	
	vector<vector<int>> dp(n, vector<int>({-INF, INF}));
	vector<vector<int>> p(n, vector<int>(2, -1));
	dp[0][0] = INF;
	dp[0][1] = -INF;
	
	for (int i = 1; i < n; ++i) {
		{
			if (a[i] > a[i - 1] && dp[i][0] < dp[i - 1][0]) {
				dp[i][0] = dp[i - 1][0];
				p[i][0] = 0;
			}
			if (a[i] < dp[i - 1][0] && dp[i][1] > a[i - 1]) {
				dp[i][1] = a[i - 1];
				p[i][1] = 0;
			}
		}
		{
			if (a[i] < a[i - 1] && dp[i][1] > dp[i - 1][1]) {
				dp[i][1] = dp[i - 1][1];
				p[i][1] = 1;
			}
			if (a[i] > dp[i - 1][1] && dp[i][0] < a[i - 1]) {
				dp[i][0] = a[i - 1];
				p[i][0] = 1;
			}
		}
	}
	
	int pos = -1;
	if (dp[n - 1][0] != -INF) {
		pos = 0;
	}
	if (dp[n - 1][1] != INF) {
		pos = 1;
	}
	
	if (pos == -1) {
		cout << "NO" << endl;
		return 0;
	}
	
	vector<int> inInc(n);
	for (int i = n - 1; i >= 0; --i) {
		if (pos == 0) {
			inInc[i] = 1;
		}
		pos = p[i][pos];
	}
	
	cout << "YES" << endl;
	for (int i = 0; i < n; ++i) {
		cout << !inInc[i] << " ";
	}
	cout << endl;
	
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一个错误信息:java.lang.IllegalArgumentException: Merged region A5 must contain 2 or more cells。这个错误的原因是合并的区域A5必须包含2个或更多的单元格。这个错误通常发生在使用Apache POI库的XSSFSheet.addMergedRegion()方法时。这个方法用于在Excel表格中添加合并的单元格区域。根据引用\[3\]中的文档解释,addMergedRegionUnsafe()方法可以跳过一些安全检验,但可能会导致创建重叠的合并区域或与多个单元格数组公式相交的合并区域,从而导致工作簿损坏。因此,为了解决这个问题,可以尝试使用addMergedRegionUnsafe()方法来添加合并的单元格区域,但需要注意可能会引发其他问题。 #### 引用[.reference_title] - *1* *3* [apache poi 高版本 3.17后合并单元格的问题](https://blog.csdn.net/qq_41578741/article/details/116794633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [单元格合并报java.lang.IllegalArgumentException](https://blog.csdn.net/spade_Kwo/article/details/128069408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值