关注
贪心
可以通过有限次的分组和重排,使得书籍编号有序。
首先,需要将书籍按照编号从小到大排序,同时记录每本书原来的位置。
排序后,遍历排序后的数组,检查相邻两本书的原始位置是否连续。如果不连续,说明这里必须要进行一次分组。
统计需要分组的次数,如果这个次数不超过给定的 ,那么就可以实现目标。
#include <iostream>
(30316)#include <vector>
#include <algorithm>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
vector<pair<long long, int>> books(n);
for (int i = 0; i < n; i++) {
cin >> books[i].first;
books[i].second = i;
}
// 按编号排序
sort(books.begin(), books.end());
int groups = 1;
for (int i = 1; i < n; i++) {
// 如果相邻两本书的原始位置不连续,需要增加一个分组
if (books[i].second - books[i-1].second != 1) {
groups++;
}
}
// 如果需要的分组数不超过k,则可以实现目标
cout << (groups <= k ? "True" : "False") << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
查看原帖
2 4
相关推荐
点赞 评论 收藏
分享
10-13 17:07
门头沟学院 Java 投票
梦语SoulGreat:第3题贪心思路,把原数组序号和数值合并按数值进行排序,满足序号和下一个序号原数组相邻,可合并数+1,只要可合并数 + k不小于n就是true
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
牛客热帖
- 1... 最有挫败感的一件事9303
- 2... 你就天天围着秋招转吧,饭也不用吃了,课也不用上了,就天天思考它到底发不发offer,把人想废,把身体想烂,把爱好全部荒废,每天就无时无刻的看邮箱有没有新邮件。你就看你***就得了。如果你还有脑子,那么不管什么时候看到这句话都该去做自己该做的事,你的肩上还有人类的希望,你的背后还有很多群友爱你。你可以继续这样把自己压垮,直到失去所有曾经引以为傲的东西——你的学习能力、你的热情、你的身体健康,甚至你的梦想。你每天守着邮箱等待的,不是成功的钥匙,而是徒增的焦虑和无尽的自责。是时候停下来,问问自己:这真的值得吗?如果你还记得那些曾经驱动你的梦想,那些让你熬夜苦读、让你为之奋斗的目标,那么你就该意识到,秋招只是你人生中一个节点,而不是全部。没有哪个Offer能定义你的一生,你也不需要为了这一时的得失,丢掉对生活和学习的热情。你是值得被爱,被认可的。那些关心你的人不希望你被秋招的压力吞噬,他们希望看到一个满怀信心、活力四射的你。肩上的责任不仅仅是一个工作机会,而是为自己和未来的生活去不断努力,保持平衡,拥有健康的身心。放下手机,关闭邮箱,去做让你真正快乐、充实的事。读书、学习、运动、发展自己的兴趣,这些都将为你的人生铺平道路。当你真正成长为一个内心强大、自信满满的人,机会自然会到来7665
- 3... 选择国央企了7229
- 4... 你跟HR谈薪资是否全面呢?6793
- 5... 听障大学生之技术处女面,卓望数码一面5045
- 6... 💔秋招被确诊为恋爱脑4478
- 7... #浪潮求职进展汇总# 一面过了但是心理测评一直没过,做了四次都没过,hr看不下去了亲自帮我把测评做了4471
- 8... 秋招结束,写个总结4075
- 9... 广东移动AI面(2024.10.18)3908
- 10... 准备和东哥做兄弟了,最快意向3750
正在热议
# offer帮选 #
148969次浏览 1598人参与
# 非技术岗薪资爆料 #
128971次浏览 1191人参与
# 平安产险科技中心求职汇总 #
46228次浏览 490人参与
# 我的简历长这样 #
1605160次浏览 25293人参与
# 韶音科技求职进展汇总 #
29990次浏览 383人参与
# 毕业季,你想好怎么跟生活对线了吗? #
129731次浏览 2752人参与
# 叠纸游戏校招 #
25299次浏览 353人参与
# 无实习如何秋招上岸 #
588231次浏览 7332人参与
# 你觉得技术面多长时间合理? #
34001次浏览 226人参与
# 0offer是寒冬太冷还是我太菜 #
733156次浏览 6878人参与
# 得物求职进展汇总 #
31562次浏览 498人参与
# 想实习转正,又想准备秋招,我该怎么办 #
417011次浏览 4472人参与
# 如果实习可以转正,你会不会放弃秋招 #
154517次浏览 2353人参与
# 写简历别走弯路 #
575113次浏览 7151人参与
# 面试中的破防瞬间 #
168858次浏览 1880人参与
# 24届秋招同行攻略分享 #
789236次浏览 12135人参与
# vivo求职进展汇总 #
122476次浏览 869人参与
# 你觉得今年秋招难吗 #
950521次浏览 13165人参与
# 没有实习经历,还有机会进大厂吗 #
726030次浏览 12905人参与
# 选了这个offer,你有没有后悔? #
257204次浏览 2263人参与
# 我的成功项目解析 #
178586次浏览 4079人参与
# 你是怎么缓解秋招焦虑的? #
79652次浏览 792人参与