当前位置:网站首页>Draw Your Cards
Draw Your Cards
2022-07-31 02:03:00 【山中一扶苏】
原题链接
题目描述
现有n张牌叠在一起,每次从最上面取出一张牌(上面的值为x),面朝上放在桌上,放置规则为:
找到已经在桌上的牌中第一个>=x的,叠在上面(更新该处的值)
若每日找到符合要求的,则新开一堆(单张牌放着)
如果某一堆摊开的牌个数达到了k,就可以把这一堆拿走
按照牌序号1-n的顺序输出该牌是什么时候被拿走的,没被拿走的牌则输出-1
输入样例
5 2
3 5 2 1 4
输出样例
4
3
3
-1
4
算法
(链式存储 + 二分查找)
模拟。set 维护每一堆牌的数量以及顶部的那张牌的值
二分查找值来实现更新
有一个不太好处理的点,就是在拿走k张牌的时候,如何找到路径呢?
那么可以用一个数组来迭代保存路径(有点像链表一样,往回找)
然后因为取牌是有顺序的,所以可以在线处理
注意:要用set自带的lower_bound,效率比 < algorithm > 头文件带的lower_bound 会高很多很多
C++ 代码
时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
vector<int> card(n + 10,-1),cnt(n + 10,0),store(n + 10,-1);
set<int> s;
for (int i = 1;i <= n;i ++ ) {
int p;
scanf("%d",&p);
auto key = s.upper_bound(p);//一定要用set自带的upper_bound
if (key == s.end()) {//end是最后一个的下一个
s.insert(p);
cnt[p] = 1;
}else {//只存最上面的值,且利用指针存下面的值
store[p] = (*key);
cnt[p] = cnt[(*key)] + 1;
s.erase(key);
s.insert(p);
}
if (cnt[p] == k) {
s.erase(p);
int x = p;
for(int j = 0;j < k;j ++){
card[x] = i;
x = store[x];//不断迭代下面的存储的值
}
}
}
for (int i = 1;i <= n;i ++ ) printf("%d\n",card[i]);
return 0;
}
边栏推荐
- Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
- ShardingJDBC usage summary
- How to design the changing system requirements
- 934. The Shortest Bridge
- [1154]如何将字符串转换为datetime
- MySQL的存储过程
- 怎样做好一个创业公司CTO?
- cudaMemcpy学习笔记
- 进程间通信学习笔记
- Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
猜你喜欢
随机推荐
Gateway routing configuration
系统需求多变如何设计
rpm install postgresql12
加密生活,Web3 项目合伙人的一天
leetcode-1161: Maximum in-layer element sum
mmdetection trains a model related command
The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
multiplayer-hlap 包有问题,无法升级的解决方案
Can an inexperienced college graduate switch to software testing?my real case
Observer mode (1)
Overview of prometheus monitoring
Fiddler captures packets to simulate weak network environment testing
Simple confession page
STP选举(步骤+案列)详解
cudaMemcpy学习笔记
The effective square of the test (one question of the day 7/29)
There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
Drools基本介绍,入门案例,基本语法
Introduction to flask series 】 【 flask - using SQLAlchemy
软件测试报告有哪些内容?