当前位置:网站首页>Draw Your Cards
Draw Your Cards
2022-07-31 02:10:00 【Yamanaka Ichifusu】
Original link
topic description
The existing n cards are stacked together, each time one card is taken from the top (the value above is x), and placed face up on the table, the placement rules are:
Find the first >=x card already on the table, stack it on top (update the value there)
If it finds a card that meets the requirements every day, open a new pile (single card)
If the number of cards revealed in a certain pile reaches k, this pile can be taken away
In the order of card number 1-n, output when the card was taken, and output -1 for cards that were not taken away
Sample input
5 23 5 2 1 4
Sample output
433-14
Algorithms
(chain storage + binary search)
Simulation.set maintains the number of cards in each pile and the value of the top card
binary search value to achieve update
There is a point that is not very easy to deal with, that is, how to find the path when taking k cards?
Then you can use an array to iterate and save the path (a bit like a linked list, look back)
And because the cards are taken in order, they can be processed online
Note: Use the lower_bound that comes with the set, which is much more efficient than the lower_bound of the
C++ code
Time Complexity O ( n l o g n ) O(nlogn) O(n
#include #include #include #include #include using namespace std;typedef pair PII;int main(){int n,k;scanf("%d%d",&n,&k);vector card(n + 10,-1),cnt(n + 10,0),store(n + 10,-1);set s;for (int i = 1; i <= n; i ++ ) {int p;scanf("%d",&p);auto key = s.upper_bound(p);//Be sure to use the upper_bound that comes with the setif (key == s.end()) {//end is the next to the lasts.insert(p);cnt[p] = 1;}else {//Only the top value is stored, and the pointer is used to store the following valuestore[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];//Iterate over the stored values below}}}for (int i = 1;i <= n;i ++ ) printf("%d\n",card[i]);return 0;}
边栏推荐
- 充电效果模拟
- What have I experienced to become a tester who is harder than development?
- Charging effect simulation
- Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
- 类似 MS Project 的项目管理工具有哪些
- 图像处理技术的心酸史
- FPGA-based vending machine
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- [1153] The boundary range of between in mysql
- GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
猜你喜欢
What have I experienced when I won the offer of BAT and TMD technical experts?
pycharm cannot run after renaming (error: can't open file...No such file or directory)
英特尔软硬优化,赋能东软加速智慧医疗时代到来
CV-Model [3]: MobileNet v2
Static routing + PAT + static NAT (explanation + experiment)
mmdetection trains a model related command
Shell script to loop through values in log file to sum and calculate average, max and min
mysql view
mmdetection训练一个模型相关命令
Nacos
随机推荐
mysql 视图
pycharm cannot run after renaming (error: can't open file...No such file or directory)
What is the ideal college life?
MySQL installation tutorial (detailed, package teaching package~)
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
静态路由+PAT+静态NAT(讲解+实验)
Problems that need to be solved by the tcp framework
Detailed explanation of STP election (step + case)
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
What have I experienced when I won the offer of BAT and TMD technical experts?
MySQL stored procedure
修改未正确放入沙盒造成苹果兼容性问题
基于FPGA的售货机
MySQL的分页你还在使劲的limit?
软件测试缺陷报告---定义,组成,缺陷的生命周期,缺陷跟踪产后处理流程,缺陷跟踪处理流程,缺陷跟踪的目的,缺陷管理工具
Nacos
Drools WorkBench的简介与使用
Static routing + PAT + static NAT (explanation + experiment)
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
两个有序数组间相加和的Topk问题