P16569 [ICPC 2026 APC] Time Display Stickers

· · 题解

这不就是 CF2206K 么。

由于第一位取 0 还是 1 影响到第二位的范围,可以把时间划分为两类:

H H M M
A 类 0 0 \sim 9 0 \sim 5 0 \sim 9
B 类 1 0 \sim 1 0 \sim 5 0 \sim 9

考虑检查能否将 x+y 张贴纸描述出时间,其中 x 张用于 A 类,y 张用于 B 类。

优先满足限制较严的位置,能给后面留出更多贴纸。按如下步骤:

  1. 将 x 个 0 贴纸分配给 A 类的第一个位置;
  2. 将 y 个 1 贴纸分配给 B 类的第一个位置;
  3. 将 y 个 0 或 1 贴纸分配给 B 类的第二个位置;
  4. 将 x+y 个 0 \sim 5 间的贴纸分配给所有时间的第三个位置;
  5. 将 2x+y 个贴纸分配给剩余位置。

过程中,若出现某种贴纸数量不够则不满足要求。

可以枚举 x,对 y 二分答案。若一种 x,y 的取值满足条件,则增加一张仍可以,故有单调性。答案取最大值。

时间复杂度 \Theta(n \log n)。

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

int t, n, a[10];

bool check(int x, int y){
    int s0 = a[0], s1 = a[1], s01 = s0 + s1, s05 = s01 + a[2] + a[3] + a[4] + a[5], s09 = s05 + a[6] + a[7] + a[8] + a[9];
    s0 -= x;
    if(s0 < 0) return 0;
    s1 -= y;
    if(s1 < 0) return 0;
    s01 -= x + y; s05 -= x + y; s09 -= x + y;
    s01 -= y;
    if(s01 < 0)
        return 0;
    s05 -= y; s09 -= y;
    s05 -= x + y;
    if(s05 < 0)
        return 0;
    s09 -= x + y;
    s09 -= x * 2 + y;
    if(s09 < 0)
        return 0;
    return 1;
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    cin >> t;
    while(t --){
        for(int i = 0; i <= 9; ++ i)
            a[i] = 0;
        cin >> n;
        for(int i = 1; i <= n; ++ i){
            char c;
            cin >> c;
            ++ a[c - '0'];
        }
        int ans = 0;
        for(int x = 0; x <= n; ++ x){
            int l = 0, r = n - x, res = -1e7;
            while(l <= r){
                int y = (l + r) >> 1;
                if(check(x, y))
                    l = y + 1, res = y;
                else
                    r = y - 1;
            }
            ans = max(ans, x + res);
        }
        cout << ans << "\n";
    }

    return 0;
}