当前位置:网站首页>很妙的贪心(F2. Nearest Beautiful Number (hard version))
很妙的贪心(F2. Nearest Beautiful Number (hard version))
2022-06-13 10:57:00 【重生之我会拧瓶盖】
题目大意是:给定一个数,定义k为这个数各位上的数字不重复的个数,让你求大于等于这个数的最小的一个数并满足k<=题目中所给的k。
很巧妙的贪心,经过分析每次从个位向高位枚举判断能修改的最低位然后加1,重复这个过程可得到答案。
#include <bits/stdc++.h>
using namespace std;
int calc(int x) {
int t = 0;
for (; x; x /= 10) t |= 1 << (x % 10);
return __builtin_popcount(t);
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
int n, k;
scanf("%d%d", &n, &k);
while (calc(n) > k) {
int a = 1, b = n;
while (calc(b) > k) a *= 10, b /= 10;
a /= 10;
n = (n / a + 1) * a;
}
printf("%d\n", n);
}
return 0;
}
边栏推荐
- 2022煤矿探放水特种作业证考试题库模拟考试平台操作
- Talk about MySQL indexing mechanism
- 我们用了一个周末,将 370 万行代码迁移到了 TypeScript
- [cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
- 《气候韧性和可持续性》| 新研究表明超级飓风未来几年会对南亚产生更大破坏
- 第六章 I/O管理作业
- Do you agree that the salary of hardware engineers is falsely high?
- Vivo large scale kubernetes cluster automation operation and maintenance practice
- Go zero microservice Practice Series (III. API definition and table structure design)
- Deploy vscode on kubernetes cluster
猜你喜欢
To vent their dissatisfaction with their superiors, Baidu post-95 programmers were sentenced to 9 months for deleting the database
记几次略有意思的 XSS 漏洞发现
实战模拟│企业微信机器人实时报错预警
Codeforces Round #798 (Div. 2)ABCD
View the default MySQL password in the pagoda
技术管理进阶——管理者可以使用哪些管理工具
5.5 clock screensaver
硬件工程师薪资虚高,你认可吗?
数据库学习笔记(第十五章)
Chapter VII document management
随机推荐
DNS协议分析
Wait for someone with "source" | openharmony growth plan student challenge registration to start
宝塔访问从IP改为域名
Actual combat analysis of malicious code lab05-01
JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制
Redis related
Codeforces Round #798 (Div. 2)ABCD
Deploy vscode on kubernetes cluster
Questions and answers of the labor worker general basic (labor worker) work license in 2022
Software testing often asks, do you really build a testing environment?
Brief introduction to memory structure of virtual machine
Go 要加个箭头语法,这下更像 PHP 了!
To vent their dissatisfaction with their superiors, Baidu post-95 programmers were sentenced to 9 months for deleting the database
PyTorch基础(二)-- 张量与梯度
Vivo large scale kubernetes cluster automation operation and maintenance practice
Initial installation and use of redis [play with Huawei cloud]
数据库系统概念(第十七章)
MySQL transaction isolation level and mvcc
C file package and download
【20220526】UE5.0.2 release d11782b