F - Absolute Minima(关于数组中 中位数的维护和添加)

2023-11-16

F - Absolute Minima


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 600600 points

Problem Statement

There is a function f(x)f(x), which is initially a constant function f(x)=0f(x)=0.

We will ask you to process QQ queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

  • An update query 1 a b: Given two integers aa and bb, let g(x)=f(x)+|x−a|+bg(x)=f(x)+|x−a|+b and replace f(x)f(x) with g(x)g(x).
  • An evaluation query 2: Print xx that minimizes f(x)f(x), and the minimum value of f(x)f(x). If there are multiple such values of xx, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

  • All values in input are integers.
  • 1≤Q≤2×1051≤Q≤2×105
  • −109≤a,b≤109−109≤a,b≤109
  • The first query is an update query.

Input

Input is given from Standard Input in the following format:

QQ
Query1Query1
::
QueryQQueryQ

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of xx that minimizes f(x)f(x), and the minimum value of f(x)f(x), in this order, with space in between.


Sample Input 1 Copy

Copy

4
1 4 2
2
1 1 -8
2

Sample Output 1 Copy

Copy

4 2
1 -3

In the first evaluation query, f(x)=|x−4|+2f(x)=|x−4|+2, which attains the minimum value of 22 at x=4x=4.

In the second evaluation query, f(x)=|x−1|+|x−4|−6f(x)=|x−1|+|x−4|−6, which attains the minimum value of −3−3 when 1≤x≤41≤x≤4. Among the multiple values of xx that minimize f(x)f(x), we ask you to print the minimum, that is, 11.


Sample Input 2 Copy

Copy

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2 Copy

Copy

-1000000000 3000000000

本质上是和东北赛一样的中位数经典应用

只是本题更多的考察是关于中位数的维护

1e5次添加 查询

那么我们使用两个优先队列

一个表示数组的前半部分另一个就表示数组的后半部分

那么我们就可以通过两个优先队列的转移来维护中位数

代码

#include<bits/stdc++.h>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> >q2;//后半部分
priority_queue<long long,vector<long long>,less<long long> >q1;//前半部分
long long sum1,sum2;//qian hou
int main()
{
    long long sum1=0,sum2=0;
    long long num=0;
    int op;
    int q;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d",&op);
        if(op==1)
        {
            long long temp1,temp2;
            scanf("%lld%lld",&temp1,&temp2);
            num+=temp2;
            if(!q1.size()||temp1<q1.top()) q1.push(temp1),sum1+=temp1;
            else q2.push(temp1),sum2+=temp1;
            //维持均匀
            while(q1.size()<q2.size())
            {
                //cout<<"*";
                long long temp;
                temp=q2.top();
                q2.pop();
                q1.push(temp);
                sum2-=temp;
                sum1+=temp;
            }
            while(q1.size()-1>q2.size())
            {
                //cout<<"2";
                long long temp;
                temp=q1.top();
                q1.pop();
                q2.push(temp);
                sum1-=temp;
                sum2+=temp;
            }
        }
        else
        {
            long long sum=q1.size()+q2.size();
            long long ans;
            if(q1.size()&&q2.size())
            ans=1LL*q1.top()*q1.size()-sum1+sum2-q1.top()*q2.size()+num;
            else if(q1.size())
            {
                ans=num;
            }
//            cout<<num<<" ";
//            cout<<sum1<<" "<<sum2<<q1.size()<<" "<<q2.size()<<endl;
            printf("%lld %lld\n",q1.top(),ans);
        }
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

F - Absolute Minima(关于数组中 中位数的维护和添加) 的相关文章

  • 创建一个popwindow 并动态设置pop的高度 限定pop高度

    创建一个popwindow 并动态设置pop的高度 限定pop高度 这里举个例子 pop里面放的是一个listview 直接上代码 SelectMedicalCasePopwindow java public class SelectMed
  • 力扣每日一题——三角形的最大周长

    题目链接 class Solution public int largestPerimeter vector
  • react仿钉钉流程图-审批工作流

    前言 此前做项目遇到一个流程图的业务场景 查找了一些资料和插件都没有找到理想的 最后找到了一款比较美观 仿钉钉流程图的 但是找来找去都找不到react版本的 只找到vue版本的 没办法 只能自己写一个 仿钉钉流程图 Api包括 一维数组传参
  • vue 自适应屏幕分辨率,在不同分辨率,以及缩放都按照设计稿展示

    项目中 会遇到这样的问题 一个网页在1920 1080的分辨率下 一屏正好展示完当前页面 但是在1366 768 或者在2k高分辨率下 页面会有滚动条 或者下方会出现空白 还有一种是14寸 或者13寸笔记本在出厂时会设置缩放125 或者15
  • C# NPOI写excel文件,设置某个单元格为自动筛选

    如标题所示 附上几行代码 HSSFWorkbook workbook new HSSFWorkbook 创建工作表 var sheet workbook CreateSheet 信息表 设置excel的自动筛选 CellRangeAddre
  • 函数局部有界性定理_高等数学入门——函数极限的基本性质

    系列简介 这个系列文章讲解高等数学的基础内容 注重学习方法的培养 对初学者不易理解的问题往往会不惜笔墨加以解释 在内容上 以国内的经典教材 同济版高等数学 为蓝本 并对具体内容作了适当取舍与拓展 例如用 语言证明函数极限这类高等数学课程不要
  • API学习笔记:2.3-2.4 API核心DLL与Unicode和多字节

    API核心DLL与Unicode和多字节 2 3 Windows核心DLL 2 3 1 核心DLL简介 2 4 Unicode和多字节 2 4 1 W版本和A版本的API 2 4 2 Unicode与ASCII的转换 前面几章基本都是总体的
  • ls: 显示目下的内容及相关属性信息

    ls 显示目下的内容及相关属性信息 功能说明 ls 命令可以理解为英文单词 list 的缩写 其功能是列出目录的内容及其内容属性信息 list directory contents 该命令有点类似于DOS系统下的dir命令 有趣的是 Lin
  • Linux内核分析:输入输出,字符与块设备 31-35

    CPU 并不直接和设备打交道 它们中间有一个叫作设备控制器 Device Control Unit 的组件 例如硬盘有磁盘控制器 USB 有 USB 控制器 显示器有视频控制器等 这些控制器就像代理商一样 它们知道如何应对硬盘 鼠标 键盘
  • 左孩子右兄弟 蓝桥杯1451 python

    题目描述 对于一棵多叉树 我们可以通过 左孩子右兄弟 表示法 将其转化成一棵二叉树 如果我们认为每个结点的子结点是无序的 那么得到的二叉树可能不唯一 换句话说 每个结点可以选任意子结点作为左孩子 并按任意顺序连接右兄弟 给定一棵包含 N 个
  • 利用cl_demo_output=>display( )实现日志的功能

    有一些客户需要对一些批处理程序进行消息日志提醒 需要用到弹窗ALV cl demo output gt display 是实现该功能最简单的方式 只需要定义一个内表就行了 如图所以 客户运行了一个库存导入的程序 将BAPI抛出的结果利用弹窗
  • debian 11搭建nis服务器

    NIS的由来与功能 NIS Network InformationService网络信息服务 用于对网络中的多台Linux系统的帐号和密码的集中管理和维护 也就是说可以用同一个帐号登录域中的Linux系统 不需要所登录的系统中存在该帐号 所
  • QT入门笔记(一)QT信号和槽

    QT入门笔记 Qt事件 Qt 事件指的是应用程序和用户之间的交互过程 例如用户按下某个按钮 点击某个输入框等等 实际上除了用户会与应用程序进行交互外 操作系统也会与应用程序进行交互 例如当某个定时任务触发时 操作系统会关闭应用程序 这也是一
  • Linux C基础——” Makefile “ 文件管理大师你拜访过嘛?

    文章目录 Make简介 Makefile基本结构 1 make是如何工作的 2 makefile文件中的依赖关系理解 3 Makefile书写规则 4 Makefile 基础的使用 3 makefile文件中的依赖关系理解 4 创建和使用变

随机推荐