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 ...
随机推荐
- CentOS编译openjdk
编译openjdk 1. 下载openjdk源码 openjdk的官网是OpenJDK (java.net) 在网站左侧就能看到它的源码位置的链接 从图上可以看到,它的源码在两个位置有托管,Mercu ...
- 树形DP详解+题目
关于树形dp 我觉得他和线性dp差不多 总结 最近写了好多树形dp+树形结构的题目,这些题目变化多样能与多种算法结合,但还是有好多规律可以找的. 先说总的规律吧! 一般来说树形dp在设状态转移方程时都 ...
- 群晖ping自动关机
因为学校宿舍环境的原因,每天早上10点左右宿管可能会来检查内务情况,然后一定会关闭电闸,就导致如果我的nas在10点半时开机的话用的是UPS的电源,直接导致UPS电量耗竭,而我又不可能一直去观察宿舍有 ...
- Nocalhost 为 KubeSphere 提供更强大的云原生开发环境
作者简介 张海立(驭势科技云平台研发总监):开源爱好者,云原生社区上海站 PMC 成员,KubeSphere Ambassador:日常云原生领域工作涉及 Kubernetes.DevOps.可观察性 ...
- 当 dotnet-monitor 遇上 Prometheus, 是种什么样的体验?
对于开发和运维人员来说, 监控大屏很棒, 让我们来做一个 Dashboard 吧! 大家可能听说过一些 CLI 诊断工具, 比如 dotnet-counters,dotnet-dump 和 dotne ...
- 洛谷 P4183 - [USACO18JAN]Cow at Large P(点分治)
洛谷题面传送门 点分治 hot tea. 首先考虑什么样的点能够对以 \(u\) 为根的答案产生 \(1\) 的贡献.我们考虑以 \(u\) 为根对整棵树进行一遍 DFS.那么对于一个点 \(v\), ...
- Codeforces 796E - Exam Cheating(dp)
Codeforces 题目传送门 & 洛谷题目传送门 当被数据结构搞自闭的 tzc 信心满满地点开一道 *2400 的 dp 题时-- 却发现自己不会做?! 这足以证明蒟蒻 dp 之菜/dk/ ...
- Codeforces 538G - Berserk Robot(乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 一道很神的乱搞题 %%% 首先注意到如果直接去做,横纵坐标有关联,不好搞.这里有一个非常套路的技巧--坐标轴旋转,我们不妨将整个坐标系旋转 ...
- Comet OJ Contest #13 D
Comet OJ Contest #13 D \(\displaystyle \sum_{i=0}^{\left\lfloor\frac{n}{2}\right\rfloor} a^{i} b^{n- ...
- OpenFOAM 中 c++ 基础
文件布置 在 OpenFOAM 中,所有代码都以注释段开头,使用有限体积的 CFD 类型文件都包括以下头文件 #include "fvCFD.H" 在此头文件种,仅包含类或函数的定 ...