跳到正文

题解

P16569 [ICPC 2026 APC] Time Display Stickers

ImmatureDreamer约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年5月21日
更新
2026年5月21日
篇幅
约 3 分钟
题目
P16569

版本

  1. v2当前
  2. v1

这不就是 CF2206K 么。

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

HHMM
A 类000∼90 \sim 90∼50 \sim 50∼90 \sim 9
B 类110∼10 \sim 10∼50 \sim 50∼90 \sim 9

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

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

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

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

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

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


C++
#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;
}
原文