AT4821 Yutori(贪心)

Description

给定一个长为 n n 的字符串,第 i i 个字符为 o 意味着第 i i 天可以工作,否则不可以。第 i i 天工作后的 i + 1 i + c i + 1 \sim i + c 天都不能工作。如果工作 k k 天,求哪几天必须工作。

1 n 2 × 1 0 5 1 \leq n \leq 2 \times 10^5

Solution

先正向求第 i i 次工作最早在 pre i \text{pre}_i 这一天,再反向求第 i i 次工作最晚在 suf i \text{suf}_i 这一天。如果 pre i = suf i \text{pre}_i = \text{suf}_i 那么第 i i 天必须工作。

时间复杂度 O ( n ) O(n)

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
inline int read() {
	int x = 0, f = 0; char ch = 0;
	while (!isdigit(ch)) f |= ch == '-', ch = getchar();
	while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
int pre[N], suf[N];
char s[N]; 
int main() {
	int n = read(), k = read(), c = read();
	scanf("%s", s + 1); int len = strlen(s + 1);
	for (int i = 1, j = 0; i <= len && j <= k; i++) if (s[i] == 'o') pre[++j] = i, i += c;
	for (int i = len, j = k; i && j; i--) if (s[i] == 'o') suf[j--] = i, i -= c;
	for (int i = 1; i <= k; i++) if (pre[i] == suf[i] && pre[i]) printf("%d\n", pre[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39984146/article/details/105451080
今日推荐