• BZOJ_2947_[Poi2000]促销_堆


    BZOJ_2947_[Poi2000]促销_堆

    Description

    Bytelandish连锁超市委托你编写一个程序来模拟一项即将施行的促销活动,该活动的规则如下:
    ●想要参与的顾客,只需把他的个人资料写在帐单上,并把帐单放入投票箱;
    ●每天活动结束时,数额最大、最小的两张帐单被取出,付款数额最大的顾客将获得一笔奖金,价值为取出的两张帐单的数额之差;
    ●为了不重复计算,取出的两张帐单不再放回箱子,而剩下的帐单仍保留在箱中,进行第二天的活动。
    超市每天的营业额很大,因此可假定:每天活动结束时,箱中至少有两张帐单以供取出。
    你的任务是根据每天投入箱中的帐单,计算出这项促销活动期间超市付出的奖金总数额。
    任务:
    编写一个程序,完成下列工作:
    ●读入投入箱中的帐单的信息;
    ●算出促销活动期间的奖金总额;

    Input

     
    第一行是一个整数 n(1 <= n <= 5000),表示促销活动历时的天数。
    以下的n行,每行包含若干由空格分隔的非负整数。第i+1行的数表示在第i天投入箱子的账单金额。每行的第一行是一个整数k(0 <= k <= 105), 表示当日账单的数目。后面的k个正整数代表这k笔账单的金额,均小于106
    整个活动中涉及到的账单笔数不会超过106

    Output

    唯一一行是一个整数,等于整个促销活动中应该付出的奖金总额。

    Sample Input

    5
    3 1 2 3
    2 1 1
    4 10 5 5 1
    0
    1 2

    Sample Output

    19


    可以,不多BB,看代码。

    代码:

    /*#include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    inline char nc() {
        static char buf[100000],*p1,*p2;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    int rd() {
        int x=0; char ch=nc();
        while(ch<'0'||ch>'9') ch=nc();
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=nc();
        return x;
    }
    #define N 1000050
    #define ls ch[p][0]
    #define rs ch[p][1]
    #define get(x) (ch[f[x]][1]==x)
    long long ans;
    int ch[N][2],f[N],rt,reimu,val[N],siz[N],n,cnt;
    int newnode(int v) {val[++reimu]=v; return reimu;}
    void pushup(int p) {siz[p]=siz[ls]+siz[rs]+1;}
    void rotate(int x) {int y=f[x],z=f[y],k=get(x);ch[y][k]=ch[x][!k];f[ch[y][k]]=y;ch[x][!k]=y;f[y]=x;f[x]=z;if(z)ch[z][ch[z][1]==y]=x;if(rt==y)rt=x;pushup(y);pushup(x);}
    void splay(int x,int y) {for(int d;(d=f[x])!=y;rotate(x)) if(f[d]!=y) rotate(get(x)==get(d)?d:x);}
    void insert(int x) {
        int l,r,p=rt;
        while(p) {
            if(val[p]<x) l=p,p=rs;
            else r=p,p=ls;
        }
        splay(l,0); splay(r,rt);
        p=newnode(x); ch[r][0]=p; f[p]=r; siz[p]=1; pushup(r); pushup(l);
    }
    int pre() {int p=ch[rt][0]; while(rs) p=rs;return p;}
    int nxt() {int p=ch[rt][1]; while(ls) p=ls;return p;}
    int find(int x) {
        int p=rt;
        while(1) {
            if(siz[ls]>=x) p=ls;
            else {
                x-=siz[ls]+1; if(!x) return p; p=rs;
            }
        }
    }
    void solve() {
        int mx,mn,p;
        p=find(2);   mn=val[p]; splay(p,0); int p1=1,p2=nxt(); splay(p1,0); splay(p2,rt); ch[p2][0]=0; pushup(p2); pushup(p1);
        p=find(cnt); mx=val[p]; splay(p,0);     p1=pre(),p2=2; splay(p1,0); splay(p2,rt); ch[p2][0]=0; pushup(p2); pushup(p1);
        ans=ans+mx-mn;
    }
    int main() {
        scanf("%d",&n);
        val[1]=-10000000; siz[1]=1; f[1]=2;
        val[2]=10000000; siz[2]=2; ch[2][0]=1; rt=2;
        reimu=2;
        int i,K,x;
        for(i=1;i<=n;i++) {
            scanf("%d",&K);
            while(K--) {
                scanf("%d",&x); insert(x); cnt++;
            }
            solve(); cnt-=2;
        }
        printf("%lld
    ",ans);
    }*/
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <ext/pb_ds/priority_queue.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    __gnu_pbds::priority_queue<int>p;
    __gnu_pbds::priority_queue<int,greater<int> >q;
    int n,vis1[1000050],vis2[1000050];
    long long ans;
    int main() {
        scanf("%d",&n);
        int K,i,x;
        for(i=1;i<=n;i++) {
            scanf("%d",&K);
            while(K--) {
                scanf("%d",&x);
                if(vis1[x]) vis1[x]--;
                else q.push(x);
                if(vis2[x]) vis2[x]--;
                else p.push(x);
            }
            while(vis1[q.top()]) vis1[q.top()]--,q.pop();
            while(vis2[p.top()]) vis2[p.top()]--,p.pop();
            ans+=p.top()-q.top(); vis1[p.top()]++; vis2[q.top()]++; q.pop(); p.pop();
        }
        printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    汉英单词对照,汉英部分
    解密SQL Server存储过程等对象
    统计信息对执行计划的影响(二)
    统计信息对执行计划的影响(一)
    asp.net 避免 ajax 定时调用,利用 ashx 实现 long polling (长轮询)
    [ADO.NET][Command]如何抓取第一筆資料的第一個欄位或scalar值?
    鱼骨图
    js 中跳出多层循环
    IIS无法 添加/编辑 应用程序扩展名映射的原因
    如何让域名后面不显示xxx.do后缀
  • 原文地址:https://www.cnblogs.com/suika/p/9186074.html
Copyright © 2020-2023  润新知