CF1802B 题解

· · 题解

思路

首先我们要把当前的猪分成两类——有性别和无性别。无性猪只能一猪一笼,有性别时最坏情况下是 \lfloor\frac s2\rfloor+1 个笼(没有有性猪时要特判)。需要的笼子就是它们之和。

代码

# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int t, n, x, sum, tot, maxx;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> n;
        maxx = sum = tot = 0;
        for (int i = 0; i < n; ++ i) {
            cin >> x;
            if (x < 2)
                ++ tot;
            else
                sum += tot, tot = 0;
            maxx = max (maxx, (sum >> 1) + tot + (sum > 0));
        }
        cout << maxx << '\n';
    }
    return 0;
}