CF85D:Sum of Medians(STL)

reference:WannaflyUnion Wechat
D. Sum of Medians
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.

sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as

The  operator stands for taking the remainder, that is  stands for the remainder of dividing x by y.

To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

Input

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.

Then each of n lines contains the description of one of the three operations:

  • add x — add the element x to the set;
  • del x — delete the element x from the set;
  • sum — find the sum of medians of the set.

For any add x operation it is true that the element x is not included in the set directly before the operation.

For any del x operation it is true that the element x is included in the set directly before the operation.

All the numbers in the input are positive integers, not exceeding 109.

Output

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cincout streams (also you may use the %I64d specificator).

Examples
input
6
add 4
add 5
add 1
add 2
add 3
sum
output
3
input
14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum
output
5
11
13
题意:

给出n个操作

.add x 表示向集合中添加x(添加x的时候保证x是第一次被添加入集合)

.del x 表示从集合中删除x (删除x的时候保证x存在于集合中)

.sum 将集合排序后,询问集合里面所有下标i % 5 = 3的元素的和(如果集合为空输出0)

思路:直接用vector做,插入和删除用二分,就不用排序了。

# include <iostream>
# include <cstdio>
# include <vector>
# include <algorithm>
using namespace std;
vector<int>v;
int main()
{
    int n, t;
    char s[10];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s);
        if(s[0] == 's')
        {
            long long ans = 0;
            for(int i=2; i<v.size(); i+=5)
                ans += v[i];
            printf("%I64d\n",ans);
        }
        else
        {
            scanf("%d",&t);
            if(s[0] == 'a')
                v.insert(lower_bound(v.begin(), v.end(), t), t);
            else
                v.erase(lower_bound(v.begin(), v.end(), t));
        }

    }
    return 0;
}


06-17
### 74LS85D 芯片概述 74LS85D 是一种 4 位二进制比较器,能够比较两个 4 位二进制数 A 和 B,并输出三个信号:A>B、A<B 和 A=B。它常用于数字电路中进行数值比较和级联操作。以下是关于 74LS85D 的详细信息[^1]。 --- ### 功能描述 74LS85D 的核心功能是将两个 4 位二进制数进行逐位比较,并根据比较结果输出以下三种状态之一: - **OA (A>B)**: 当 A 大于 B 时,该引脚输出高电平。 - **OB (A<B)**: 当 A 小于 B 时,该引脚输出高电平。 - **OC (A=B)**: 当 A 等于 B 时,该引脚输出高电平。 此外,74LS85D 支持通过级联输入引脚(I1、I2、I3)实现多位数的比较。例如,可以将多个 74LS85 芯片串联起来以比较更长的二进制数[^1]。 --- ### 引脚图与说明 以下是 74LS85D 的典型引脚排列及其功能说明: | 引脚编号 | 引脚名称 | 描述 | |----------|----------|----------------------------------------------------------------------| | 1 | I1 | 级联输入,用于表示低位比较结果为 A>B。 | | 2 | I2 | 级联输入,用于表示低位比较结果为 A<B。 | | 3 | I3 | 级联输入,用于表示低位比较结果为 A=B。 | | 4 | A0 | 输入 A 的最低有效位。 | | 5 | A1 | 输入 A 的次低有效位。 | | 6 | A2 | 输入 A 的第三位。 | | 7 | A3 | 输入 A 的最高有效位。 | | 8 | GND | 接地端。 | | 9 | B3 | 输入 B 的最高有效位。 | | 10 | B2 | 输入 B 的第三位。 | | 11 | B1 | 输入 B 的次低有效位。 | | 12 | B0 | 输入 B 的最低有效位。 | | 13 | OA | 输出,表示 A>B。 | | 14 | OB | 输出,表示 A<B。 | | 15 | OC | 输出,表示 A=B。 | | 16 | Vcc | 电源正极,通常接 +5V。 | --- ### 数据表关键参数 以下是 74LS85D 的一些重要电气特性[^1]: - **供电电压**: 4.75V 至 5.25V - **工作温度范围**: -55°C 至 +125°C - **传播延迟时间**: 典型值为 18ns(在 Vcc=5V 下) - **功耗**: 每个门的最大功耗约为 20mW --- ### Verilog 实现示例 以下是基于 74LS85D 功能的 Verilog 实现代码,展示了如何用硬件描述语言模拟其行为[^3]: ```verilog module Compare_74LS85( input wire A0, A1, A2, A3, // 输入 A 的 4 位 input wire B0, B1, B2, B3, // 输入 B 的 4 位 input wire I1, I2, I3, // 级联输入 output reg OA, OB, OC // 输出信号 ); wire[3:0] A, B; // 拼接输入信号为 4 位 assign A = {A3, A2, A1, A0}; assign B = {B3, B2, B1, B0}; always @(*) begin if (A > B) begin OA = 1; OB = 0; OC = 0; end else if (A < B) begin OA = 0; OB = 1; OC = 0; end else if (I1 & !I2 & !I3) begin OA = 1; OB = 0; OC = 0; end else if (!I1 & I2 & !I3) begin OA = 0; OB = 1; OC = 0; end else if (!I1 & !I2 & I3) begin OA = 0; OB = 0; OC = 1; end else if (I3) begin OA = 0; OB = 0; OC = 1; end else if (I1 & I2 & !I3) begin OA = 0; OB = 0; OC = 0; end else if (!I1 & !I2 & !I3) begin OA = 0; OB = 0; OC = 1; end end endmodule ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值