• F


    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
    ",q1.top(),ans);
            }
        }
    }
    
  • 相关阅读:
    lua的多种实现方式(1-100的和)
    51单片机交通灯(定时器+38译码器+中断)
    51单片机定时器实现LED闪烁
    51单片机0号与1号外部中断实例
    51单片机:IO口扩展芯片用法(74HC165,74HC595)
    mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    Intellij IDEA运行报Command line is too long解法
    Jmeter命令行运行实例讲解
    Windows10在当前目录快速打开cmd的方法
    Jmeter接口测试对json串中的值进行断言
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852262.html
Copyright © 2020-2023  润新知