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. 权限树的制作(menu)

    原来demo 实体类:get.set 1.使用递归的方式将数据查询出来. 2.为了减少数据库交互,一次查询所有数据,遍历集合,然后先判断父节点,如果不是父节点,用原集合重新遍历 3.map形式减少遍历 ...

  2. GPG 使用指南

    加密与签名 在传输信息时,会面临两个典型的问题: 如何保证发出的消息,只能被预期的接收人获取? 如何保证收到的消息,确实由预期的发送人发出? 这两个问题不难理解.例如发送的邮件可能会被监听,诈骗分子可 ...

  3. 常用的分布式ID生成器

    为何需要分布式ID生成器 **本人博客网站 **IT小神 www.itxiaoshen.com **拿我们系统常用Mysql数据库来说,在之前的单体架构基本是单库结构,每个业务表的ID一般从1增,通过 ...

  4. Java培训机构如何选择才能避免被骗?

    近年来,随着IT行业的快速崛起,各类互联网人才供不应求,而Java工程师作为目前最为火爆的岗位之一,更是以高薪+高新技术的标签受到了人们的广泛关注.许多年轻人也看到了这个行业的发展前景,决定报名培训机 ...

  5. vue项目中使用 SheetJS / js-xlsx 导出文件

    1.  npm install xlsx 2. 在App.vue 中引入xlsx import * as XLSX from 'xlsx'; // 数据导出导入所需要的依赖  3.  使用xlsx 3 ...

  6. Run For Beer CF575G

    Run for beer CF 575G 如果直接bfs分层贪心可以做,但是很毒瘤,具体可以参考Gavinzheng的提交 考虑魔改dijkstra 首先,每次拿权值最小的来松弛肯定没有问题,只是怎么 ...

  7. 1.TwoSum-Leetcode

    #include<iostream> #include<algorithm> #include<map> using namespace std; class So ...

  8. 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2

    框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...

  9. Go语言核心36讲(Go语言实战与应用二十二)--学习笔记

    44 | 使用os包中的API (上) 我们今天要讲的是os代码包中的 API.这个代码包可以让我们拥有操控计算机操作系统的能力. 前导内容:os 包中的 API 这个代码包提供的都是平台不相关的 A ...

  10. web必知,多终端适配

    导读 移动端适配,是我们在开发中经常会遇到的,这里面可能会遇到非常多的问题: 1px问题 UI图完美适配方案 iPhoneX适配方案 横屏适配 高清屏图片模糊问题 ... 上面这些问题可能我们在开发中 ...