题意(引用):题意:有很多种草,有两个属性:价格和新鲜度;有很多牛,它们都会各自需求一种草,要求是其价格最低为ai,新鲜度最低为bi,且这些牛不希望自己的草和别人的一样。问要满足所有需求的最小花费是多少?

一开始想的都是各种匹配,结果正解是贪心……

应该来说想不到好方法时,不是二分答案就是贪心了吧?

先按新鲜度为第一关键字,价格为第二关键字排序

从最挑剔(新鲜度要求越高)的牛开始考虑,每次选择应当是价格最小并且能能满足当前牛的牧草

可以这样想:当前处理的牛使没选过的牛中最挑剔的,新鲜度能满足当前牛的一定也能满足之前牛

再来考虑价格,因为价格是最低要求,这次价格选择最低限度的,显然会为之后的选择打开更多空间

所以每次每次选择应当是价格最小并且能能满足当前牛的牧草是最优的

朴素模拟复杂度O(nm) ,所以需要各种数据结构优化

考虑到好久没有写平衡树(其实是一开始想到的就是平衡树)

于是就写了splay

 var cv,cw,v,w,d,fa:array[..] of longint;
    son:array[..,..] of longint;
    root,i,j,p,n,m,t,h:longint;
    ans:int64;
function succ(x:longint):longint;   //找后继
  var p:longint;
  begin
    p:=son[x,];
    while son[p,]<> do  p:=son[p,];
    exit(p);
  end; procedure rotate(x,w:longint);
  var y:longint;
  begin
    y:=fa[x];
    if fa[y]<> then
    begin
      if son[fa[y],]=y then son[fa[y],]:=x
      else son[fa[y],]:=x;
    end;
    fa[x]:=fa[y];
    son[y,-w]:=son[x,w];
    if son[x,w]<> then fa[son[x,w]]:=y;
    son[x,w]:=y;
    fa[y]:=x;
  end; procedure splay(x:longint);     //拍习惯了就快了,还是那句话在哪里就往反向转
  var y:longint;
  begin
    while fa[x]<> do
    begin
      y:=fa[x];
      if fa[y]= then
      begin
        if son[y,]=x then rotate(x,)
        else rotate(x,);
      end
      else begin
        if son[fa[y],]=y then
        begin
          if son[y,]=x then
          begin
            rotate(y,);
            rotate(x,);
          end
          else begin
            rotate(x,);
            rotate(x,);
          end;
        end
        else begin
          if son[y,]=x then
          begin
            rotate(x,);
            rotate(x,);
          end
          else begin
            rotate(y,);
            rotate(x,);
          end;
        end;
      end;
    end;
    root:=x;
  end; procedure delete(x:longint);    //删除写得比较丑陋
  var p,y,q,u:longint;
  begin
    y:=fa[x];
    if y= then q:=
    else if son[y,]=x then q:=
    else if son[y,]=x then q:=;
    if (son[x,]<>) and (son[x,]<>) then
    begin
      p:=succ(x);
      if (son[p,]<>) and (fa[p]<>x) then
      begin
        son[fa[p],]:=son[p,];
        fa[son[p,]]:=fa[p];
      end
      else if fa[p]<>x then son[fa[p],]:=;
      son[p,]:=son[x,];
      fa[son[x,]]:=p;
      if fa[p]<>x then
      begin
        son[p,]:=son[x,];
        fa[son[x,]]:=p;
      end;
      if y<> then son[y,q]:=p;
      fa[p]:=y;
      splay(p);
    end
    else begin
      if y<> then
      begin
        if son[x,]<> then
        begin
          son[y,q]:=son[x,];
          fa[son[x,]]:=y;
        end;
        if son[x,]<> then
        begin
          son[y,q]:=son[x,];
          fa[son[x,]]:=y;
        end;
      end;
      if son[x,]<> then u:=son[x,] else u:=son[x,];
      if y= then
      begin
        root:=u;
        fa[u]:=;
      end
      else splay(y);
    end;
    dec(h);
    fa[x]:=;
    son[x,]:=;
    son[x,]:=;
    d[x]:=;
  end; procedure insert(x:longint);
  var p:longint;
  begin
    inc(t);
    inc(h);
    d[t]:=x;
    if h= then
    begin
      root:=;
      fa[t]:=;
    end
    else begin
      p:=root;
      repeat
        if d[p]>=x then
        begin
          if son[p,]= then break;
          p:=son[p,];
        end
        else begin
          if son[p,]= then break;
          p:=son[p,];
        end;
      until false;
      fa[t]:=p;
      if d[p]>=x then son[p,]:=t else son[p,]:=t;
      splay(t);
    end;
  end; procedure swap(var a,b:longint);
  var c:longint;
  begin
    c:=a;
    a:=b;
    b:=c;
  end; procedure sortc(l,r:longint);
  var i,j,x,y: longint;
  begin
    i:=l;
    j:=r;
    x:=cv[(l+r) shr ];
    y:=cw[(l+r) shr ];
    repeat
      while (cv[i]<x) or ((cv[i]=x) and (cw[i]<y)) do inc(i);
      while (x<cv[j]) or ((cv[j]=x) and (cw[j]>y)) do dec(j);
      if not(i>j) then
      begin
        swap(cv[i],cv[j]);
        swap(cw[i],cw[j]);
        inc(i);
        j:=j-;
      end;
    until i>j;
    if l<j then sortc(l,j);
    if i<r then sortc(i,r);
  end; procedure sort(l,r:longint);
  var i,j,x,y: longint;
  begin
    i:=l;
    j:=r;
    x:=v[(l+r) shr ];
    y:=w[(l+r) shr ];
    repeat
      while (v[i]<x) or ((v[i]=x) and (w[i]<y)) do inc(i);
      while (x<v[j]) or ((v[j]=x) and (w[j]>y)) do dec(j);
      if not(i>j) then
      begin
        swap(w[i],w[j]);
        swap(v[i],v[j]);
        inc(i);
        j:=j-;
      end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
  end;
begin
  readln(n,m);
  for i:= to n do
    readln(cw[i],cv[i]);
  for i:= to m do
    readln(w[i],v[i]);
  if m<n then
  begin
    writeln(-);
    halt;
  end;
  sortc(,n);
  sort(,m);
  j:=m;
  t:=;
  root:=;
  fillchar(son,sizeof(son),);
  fillchar(fa,sizeof(fa),);
  for i:=n downto do
  begin
    while v[j]>=cv[i] do
    begin
      insert(w[j]);
      dec(j);
    end;
    insert(cw[i]);
    p:=succ(t);
    if p<> then
    begin
      ans:=ans+d[p];
      delete(t);
      delete(p);
    end
    else begin
      ans:=-;
      break;
    end;
  end;
  writeln(ans);
end.

话说splay终于写对了还是1Y,好高兴

漫长的补结题报告之路 poj3622的更多相关文章

  1. 《基于Arm实验箱的国密算法应用》课程设计 结题报告

    <基于Arm实验箱的国密算法应用>课程设计 结题报告 小组成员姓名:20155206赵飞 20155220吴思其 20155234昝昕明 指导教师:娄嘉鹏 设计方案 题目要求:基于Arm实 ...

  2. 《基于Cortex-M4的ucOS-III的应用》课程设计 结题报告

    <基于Cortex-M4的ucOS-III的应用>课程设计 结题报告 小组成员姓名:20155211 解雪莹 20155217 杨笛 20155227 辜彦霖 指导教师:娄嘉鹏 一.设计方 ...

  3. [置顶] 白话最小边覆盖总结--附加 hdu1151结题报告

    刚开始看到这个题目的时候就觉得想法很明了,就是不知道如何去匹配... 去网上看了不少人的解题报告,但是对于刚接触“最小边覆盖”的我来说....还是很困难滴....于是自己又开始一如以往学习“最大独立集 ...

  4. hdu1281结题报告

    哎哎...自己刚刚一看到这个题目居然.....什么都想不到...看了一下别人的解题报告说最大匹配...于是就自己开始构思啦... 对于这个棋盘,有K个可以放棋子的位置....那么 首先我们开始可以求出 ...

  5. 2013山东省ICPC结题报告

    A.Rescue The Princess 已知一个等边三角形的两个顶点A.B,求第三个顶点C,A.B.C成逆时针方向. 常规的解题思路就是用已知的两个点列出x,y方程,但这样求出方程的解的表达式比较 ...

  6. uva401 - Palindromes结题报告

    题目地址 :  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 有向图强连通分支的Tarjan算法讲解 + HDU 1269 连通图 Tarjan 结题报告

    题目很简单就拿着这道题简单说说 有向图强连通分支的Tarjan算法 有向图强连通分支的Tarjan算法伪代码如下:void Tarjan(u) {dfn[u]=low[u]=++index//进行DF ...

  8. 2016noipday1t1玩具迷题结题报告

    经常读这个代码有益于比赛时想起一些思路.... day1t1,洛谷dalao称之为水题...??然后我去年还是没拿到分,就这个,我还就写了40%的数据,AC到40,然而这不是关键,注释了freopen ...

  9. 2017 五一 清北学堂 Day1模拟考试结题报告

    预计分数:100+50+50 实际分数:5+50+100 =.= 多重背包 (backpack.cpp/c/pas) (1s/256M) 题目描述 提供一个背包,它最多能负载重量为W的物品. 现在给出 ...

随机推荐

  1. Hello World for U (20)

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  2. 在fedora 桌面上添加应用程序

    在网上下了个android studio,这个程序只是的压缩包,解压后程序也只能在SHELL下敲入studio.sh才能运行 能不能向其他程序一样,在fedora桌面上找到应用程序,点击执行呢.在网上 ...

  3. gc overhead limit exceeded

    eclipse-- gc overhead limit exceeded 修改内存不足的方法如下: Eclipse报错:gc overhead limit exceeded eclipse 原因是Ec ...

  4. 二、mysql数据类型

    .数值型 ) decimal最大支持65位 ) 最大支持10位的二进制存储模式 bin(column),hex(column) 使用bin(二进制查看)或hex(十六进制查看)查看bit类型数据 .时 ...

  5. 高仿猫眼电影选座(选票)模块-b

    上图看效果先: 1)画座位图其实不是很难一般数据都会给坐标,将坐标对应座位画出来就可以了,主要是开场动画要设置默认大小,还有座位图的数量也不是固定的,所以在初始化座位图的时侯就默认算出了整个座位图的大 ...

  6. C++内存泄露调试

    我在看DirectX Sample的时候,看到以下代码: // Enable run-time memory check for debug builds. #if defined(DEBUG) | ...

  7. 指针 取地址& 解引用 *

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtAAAACNCAIAAAARutrLAAAgAElEQVR4nOydd3wcxd3/R13uvdsUY2

  8. PAT-乙级-1043. 输出PATest(20)

    1043. 输出PATest(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个长度不超过10000 ...

  9. uva 437 hdu 1069

    dp  将石块按三个面存入队列  按底面积排序  dp就最大高度  按嵌套矩形最长路做做法 #include <iostream> #include <cstdio> #inc ...

  10. spoj 1436

    用并查集看一下是否会围成一个环  若围成环直接输出NO   当然 当 m >= n  时必然会围成环直接输出NO #include <algorithm> #include < ...