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 题解的更多相关文章

  1. LeetCode Design Log Storage System

    原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/ 题目: You are given sever ...

  2. 算法与数据结构基础 - 广度优先搜索(BFS)

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...

  3. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  4. [LeetCode]题解(python):107 Binary Tree Level Order Traversal II

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...

  5. [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...

  6. [LeetCode]题解(python):102 Binary Tree Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...

  7. 【题解】【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, ...

  8. leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  9. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. 使用postman对elasticsearch接口调用

    post 新增 get 查询 put更新 post http://127.0.0.1:9200/index4/type1 {"node":0} { "_index&quo ...

  2. 用idea生成javadoc在线文档

    1.点击需要生成文档的包 2.点击tools--->选择generate javaDoc /1选择输出目录 /2防止中文乱码建议加上: -encoding utf-8 -charset utf- ...

  3. [CCC​2019] Tourism题解

    我们先考虑一下拿部分分: subtask1 考虑因为 \(n < 2k\) ,那么我们的划分一定是从中间某个地方裁开,且满足 \(k\) 的条件的,我们发现当划分点在 \([n\ mod\ k, ...

  4. Python中关于join函数的陷阱?

    目录 说明 数据说明 正确示例 错误示例 解决办法 说明 最近在用Python的join函数连接多个列表时,出现了如下两个错误,即合并类型不一致.折腾了很久才找到原因,真是基础不牢,地动山摇. Typ ...

  5. kubernetes 用到的工具及组件

    kubernetes 用到的工具及组件,将所有的组件下载后放到/usr/local/bin目录下(记得chmod a+x /usr/local/bin/*).所有的组件,原则上都用最新的,如果遇到不支 ...

  6. 初学者如何吃透一个Java项目

    不少初学者朋友在学习Java过程中,会对着视频敲Java项目,其中遇到的BUG还能解决,但就是每次敲完一个项目,就感觉很空虚,项目里面的知识点感觉懂了但又好像没懂 这些朋友应该怎样才能掌握一个项目所用 ...

  7. 学习java的第十六天

    一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...

  8. 学习java 7.13

    学习内容: 一个汉字存储:如果是GBK编码,占用2个字节:如果是UTF-8编码,占用3个字节 汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数 字符流=字节流+编码表 采用何种规则编码,就要 ...

  9. Factorization

    Factorization or factoring consists of writing a number or another mathematical object as a product ...

  10. django中的filter(), all(), get()

    1. 类名.objects中的get(), filter(), all() 的区别 结论: (1)all()返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使 ...