Majority Opinion

GitHub跳转原题关系图返回列表

一种干草可行当且仅当它在相邻或隔一位的位置重复出现,扫描长度 2/3 局部窗口即可。

OJ: usaco

题目 ID: 1371

难度:普及-

标签:模拟枚举

日期: 2026-07-11 12:50

题意

NN 头牛,每头牛喜欢一种干草 h[i]

一次操作可以选择一个连续区间开焦点小组。 如果这个区间里某种干草的数量严格超过一半,那么区间内所有牛都会改成喜欢这种干草;否则没有变化。

可以操作任意多次。要求输出所有可能让全体牛最终都喜欢的干草类型。

思路

暴力想法

对一种干草 x,可以反复枚举所有区间。 如果某个区间里 x 严格超过一半,就把这个区间全部改成 x

这个暴力适合小数据和对拍:

cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-11 12:50
 * update_at: 2026-07-11 12:51
 */
// brute.cpp:小数据暴力解,用来帮助理解题意并辅助对拍。
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 15;

int n;
int h[MAXN];

bool all_become(vector<int> a, int x) {
    bool changed = true;
    while (changed) {
        changed = false;
        for (int l = 1; l <= n; l++) {
            for (int r = l; r <= n; r++) {
                int cnt = 0;
                for (int i = l; i <= r; i++) {
                    if (a[i] == x) {
                        cnt++;
                    }
                }

                int len = r - l + 1;
                if (cnt * 2 > len && cnt < len) {
                    for (int i = l; i <= r; i++) {
                        a[i] = x;
                    }
                    changed = true;
                }
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        if (a[i] != x) {
            return false;
        }
    }
    return true;
}

void solve_one() {
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        a[i] = h[i];
    }

    bool printed = false;
    for (int x = 1; x <= n; x++) {
        if (all_become(a, x)) {
            if (printed) {
                cout << ' ';
            }
            cout << x;
            printed = true;
        }
    }
    if (!printed) {
        cout << -1;
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve_one();
    }

    return 0;
}

满分数据中不能对每种干草反复枚举所有区间,需要找出更小的判定条件。

局部多数

一种干草能开始扩张,必须先在某个区间里形成严格多数。

最小的有效局部结构只有两种:

text
x x
x ? x

第一种是相邻两个相同。 第二种是在长度为 3 的区间中出现 2 次,严格超过一半。

如果有这两种结构之一,就可以先把这个局部区间都变成 x,之后不断把旁边一头牛并入长度 3 的区间中,继续扩张到整个数组。

反过来,如果一个更长区间中 x 严格超过一半,那么这个区间里一定存在两个 x 的距离不超过 2。 否则每连续 3 个位置最多只有一个 x,不可能超过一半。

所以只需要检查:

text
h[i] == h[i-1]
h[i] == h[i-2]

满足任一条件的干草类型就是可行类型。

代码

cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-11 12:50
 * update_at: 2026-07-11 12:51
 */
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

int n;
int h[MAXN];
bool good[MAXN];

void solve_one() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        good[i] = false;
    }

    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        if (i >= 2 && h[i] == h[i - 1]) {
            good[h[i]] = true;
        }
        if (i >= 3 && h[i] == h[i - 2]) {
            good[h[i]] = true;
        }
    }

    bool printed = false;
    for (int x = 1; x <= n; x++) {
        if (good[x]) {
            if (printed) {
                cout << ' ';
            }
            cout << x;
            printed = true;
        }
    }
    if (!printed) {
        cout << -1;
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve_one();
    }

    return 0;
}

复杂度

每组数据扫描数组一次,再扫描编号输出答案,时间复杂度 O(N)O(N)

空间复杂度 O(N)O(N)

总结

这题的关键是把“任意长区间的严格多数”压缩成长度 2 和长度 3 的局部检查。

存在 x xx ? x,就能作为扩张起点;不存在这样的局部结构,就不可能形成严格多数。