CF572B Order Book 题解
Content
账单里面有 \(n\) 条记录,只有卖出记录和买入记录两种,并且都包含两个信息 \(p_i,q_i\),现在根据这些记录,请执行如下操作:
- 将所有 \(p_i\) 相等的同种记录合并(就是只能卖出记录和卖出记录合并,买入记录和买入记录合并,不能将卖出记录和买入记录合并)。
- 合并之后,将所有 \(p_i\) 最小的 \(s\) 条卖出记录按照 \(p_i\) 降序输出。将所有 \(p_i\) 最大的 \(s\) 条买入记录按照 \(p_i\) 降序输出。
笔者提醒:买入卖出记录可能不足 \(s\) 条,这时将所有的记录按照 \(p_i\) 降序输出。
数据范围:\(1\leqslant n\leqslant1000,1\leqslant s\leqslant 50,0\leqslant p_i\leqslant 10^5,1\leqslant q_i\leqslant 10^4\)。
Solution
\(n\) 很小,所以我们考虑直接 \(\mathcal{O}(n^2)\) 遍历,看前面是否有 \(p_i\) 和当前的 \(p_i\) 相等,有的话直接合并,否则新开一个存储。然后卖出记录按照从小到大排序后倒序输出,买入记录按照从大到小排序后正序输出即可。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int n, s, socnt, bucnt, visso[100007], visbu[100007];
struct node {
int x, y;
bool operator < (const node& cjy) const {return x < cjy.x;}
}so[1007];
struct node2 {
int x, y;
bool operator < (const node2& cjy) const {return x > cjy.x;}
}bu[1007];
int main() {
scanf("%d%d", &n, &s);
for(int i = 1; i <= n; ++i) {
char opt[2]; int p, q;
scanf("%s%d%d", opt, &p, &q);
if(opt[0] == 'B') {
if(visbu[p]) {
for(int j = 1; j <= bucnt; ++j)
if(bu[j].x == p) {
bu[j].y += q;
break;
}
}
else {
bu[++bucnt] = (node2){p, q};
visbu[p] = 1;
}
} else {
if(visso[p]) {
for(int j = 1; j <= socnt; ++j)
if(so[j].x == p) {
so[j].y += q;
break;
}
}
else {
so[++socnt] = (node){p, q};
visso[p] = 1;
}
}
}
sort(so + 1, so + socnt + 1);
sort(bu + 1, bu + bucnt + 1);
for(int i = min(socnt, s); i >= 1; --i)
printf("S %d %d\n", so[i].x, so[i].y);
for(int i = 1; i <= min(bucnt, s); ++i)
printf("B %d %d\n", bu[i].x, bu[i].y);
return 0;
}
CF572B Order Book 题解的更多相关文章
- LeetCode Design Log Storage System
原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/ 题目: You are given sever ...
- 算法与数据结构基础 - 广度优先搜索(BFS)
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- [LeetCode]题解(python):107 Binary Tree Level Order Traversal II
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...
- [LeetCode]题解(python):102 Binary Tree Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...
- 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- 【Tool】MySQL安装
MySQL安装 2019-11-07 14:30:32 by冲冲 本机 Windows7 64bit,MySQL是 mysql-8.0.18-winx64.zip. 1.官网下载 https:// ...
- ppt的作用
ppt不重要,使用ppt的场合很重要. 演讲分享,答辩总结,商业竞标,新年计划,年终总结.
- canvas 基本介绍
# canvas 基本功能介绍 - canvas 能做什么 1. 绘制简单图形线条 2. 裁剪图片 - 开始绘制画布 新建html文档添加 canvas标签 ```html <div style ...
- 毕业设计之zabbix集合
lnmp环境请查看https://www.cnblogs.com/betterquan/p/12285956.html 但是!!!注意php的编译: https://www.zabbix.com/do ...
- 45-Letter Combinations of a Phone Number
Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution Total Accepted: 7855 ...
- MapReduce04 框架原理Shuffle
目录 2 MapReduce工作流程 3 Shuffle机制(重点) 3.1 Shuffle机制 3.2 Partition分区 默认Partitioner分区 自定义Partitioner分区 自定 ...
- RocketMQ集群搭建方式
各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...
- STM32 CAN用队列缓冲接收的例子
[1]CAN接收用队列缓冲的例子: 发单帧没有问题,多帧或者连续发两帧就有问题.
- Fragment放置后台很久(Home键退出很长时间),返回时出现Fragment重叠解决方案
后来在google查到相关资料,原因是:当Fragment长久不使用,系统进行回收,FragmentActivity调用onSaveInstanceState保存Fragment对象.很长时间后,再次 ...
- centos 7 重新获取IP地址
1.安装软件包 dhclient # yum install dhclient 2.释放现有IP # dhclient -r 3.重新获取 # dhclient 4.查看获取到到IP # ip a