Codeforce 547B - Mike and Feet

本文详细解析了如何通过计算每个数字左右覆盖长度,求出数组中各长度区间最小值的最大值。采用前后扫描策略,结合单调递减特性,优化算法至高效实现。

题意:给定一个数组,对于这个数组构造n个长度为1到n的区间,要求最小值最大,输出构造区间的最小值。

转换一下题意,可以求每一个数字是多长区间的最小值。
先正着求这个数字的左半边覆盖长度,再倒着求右半边覆盖长度。

然后用r - l + 1算出区间长度,对于该长度的区间取最大值。

这样搞很显然会有许多区间还没有答案,但是我们可以想到,长的区间都有答案,那短的区间也会有答案,短的区间的答案只会大于等于长的区间。因此可以用长的区间去更新短的区间。

这样复杂度可能会去到O(n²)O(n²)O(n²)。但是根据上面的结论,即短区间的答案只会大于等于长区间,所以答案序列一定是一个单调递减的序列,所以我们可以直接从后往前更新最大值。

嗯,后缀自动机经常这么干,没想到用到了这里。

ac代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
int n, a[maxn];
int l[maxn], r[maxn];
int ans[maxn];

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	for(int i = 1; i <= n; i++) {
		l[i] = i;
		while(a[l[i] - 1] >= a[i]) {
			l[i] = l[l[i] - 1];
		}
	}
	for(int i = n; i >= 1; i--) {
		r[i] = i;
		while(a[r[i] + 1] >= a[i]) {
			r[i] = r[r[i] + 1];
		}
	}
	for(int i = 1; i <= n; i++) {
		int e = r[i] - l[i] + 1;
		ans[e] = max(ans[e], a[i]);
	}
	for(int i = n - 1; i >= 1; i--) {
		ans[i] = max(ans[i], ans[i + 1]);
	}
	for(int i = 1; i <= n; i++) {
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}
### Codeforces Problem 797B Explanation The problem titled "Restoring the Permutation" requires reconstructing a permutation from its prefix sums modulo \( m \). Given an array of integers representing these prefix sums, one must determine whether it is possible to restore such a permutation. In this context, a **permutation** refers to an ordered set containing each integer exactly once within a specified range. The task involves checking if there exists any valid sequence that matches the provided conditions when performing operations as described in the problem statement[^1]. To solve this issue effectively: - Iterate through all elements while maintaining two variables: `current_sum` which tracks cumulative sum during iteration; and `min_value`, used later for adjustments. ```cpp int n, m; cin >> n >> m; vector<int> s(n); for (auto& x : s) cin >> x; ``` Calculate differences between consecutive terms after adjusting initial values appropriately by subtracting minimum value found so far at every step. This adjustment ensures non-negativity throughout calculations without altering relative order among elements. Check feasibility based on properties derived from constraints given in the question text. Specifically, ensure no duplicate residues appear under modulus operation since they would violate uniqueness required for permutations. Finally, construct answer using adjusted difference list obtained previously along with necessary checks ensuring correctness according to rules outlined above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值