题解:P16569 [ICPC 2026 APC] Time Display Stickers
题解:P16569 [ICPC 2026 APC] Time Display Stickers
题目传送门。
1.先用桶统计每个数出现的次数。
2.把数字分类。
-
小时第一位:
0\sim1 -
小时第二位:
0\sim9 -
分钟第一位:
0\sim5 -
分钟第二位:
0\sim9 因此只要分三类
3.二分。
check 用贪心,先尽量让时间是
(其余解释见代码)。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,a[1010],x,y,ans;
string s;
bool check(int d){//判断是否可行
int a1=max(0LL,d-a[0]),//需要多少个 1 来代替 0
a2=min(d,a[1]),//如果 a[1]>d ,则只需要 d 个
a3=min(x-d,y-2*d);
return min(a2,a3)>=a1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>s;
for(int i=0;i<n;i++){
a[s[i]-'0']++;//桶
}
x=a[0]+a[1];
y=a[0]+a[1]+a[2]+a[3]+a[4]+a[5];
int l=0,r=n/4;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}if(check(r))cout<<r;//尽量取大的
else cout<<l;//否则取小的
cout<<'\n';
for(int i=0;i<=9;i++){
a[i]=0;//清空
}
}
return 0;
}