复制代码

堆:主要用于解决TOPK 问题
 
我说一下使用上的问题:具体怎么定义,请查看一本神奇的书籍 数据结构 
为了方便 ,直接介绍 stl : 堆的问题
  首先 根据 数据 范围 进行 的建立 堆 :  make_heap(begin ,end, _comp)
  从 begin 到 end 建立一个堆 注意stl 的套路 [   ) ,
  默认大堆  ,小堆 请传递 greater<int>()
  push_heap(begin,end)    添加元素
  pop_heap(begin,end)   删除元素
  sort_heap (begin,end) 堆排序,添加完元素,已进行了堆顺序的调整
               这个排序 相当于快速 sort
  
  优先队列:
      priority_queue <int,vector<int>,greater<int> > q;
  greater<int> 是存放在 functional 结构体对象
   greater<int>() 函数对象
        还有一点一定要说明一下,对于结构体需要重在函数对象,一定是 bool operator < (const node &   )const 
     常函数 ,不解释  
 
 
#include<bits/stdc++.h>
using namespace std; 

struct node{
    int v,ai,bi;
    bool operator <(const node & b)const {return v>b.v;}
};



int n;
int a[100005],b[100005];
priority_queue<node>q;

int main(){
    int i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n;i++)scanf("%d",&b[i]);

    for(i=1;i<=n;i++)q.push((node){a[1]+b[i],1,i});

    node t;
    for(i=1;i<=n;i++){
        t=q.top();
        printf("%d ",t.v);
        q.pop();
        q.push((node){a[t.ai+1]+b[t.bi],t.ai+1,t.bi});
    }
 
    return 0;
}
View Code

 

 
 
#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
#define FASTIO  ios::sync_with_stdio(false);
#define CLOCK cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";


const int   inf = 987654321;
const int    sz = (int)1e6 + 5;
const int   mod = (int)1e9 + 7;
const int sqrtn = 300; 

//#define add(u,v,w) (e[++tot]=(edge){v,head[u],1},head[u]=tot;) 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl

#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)


#define all(x) x.begin(),x.end()
#define low(x) (x)&(-x)
#define pb push_back
typedef long long ll; 
typedef double dl; 

struct node
{
    int val,x,id;
    bool operator < (const node & r)
    {
        return val<r.val;
    }
    bool operator > (const node & r)
    {
        return val>r.val;
    }
}e[sz];
int heap_size ;

int n,m,a[sz],b[sz],c[sz] ;
void CHANGE(int l,int r)
{
    swap(e[l],e[r]);
}
void MIN_HEAPIFY(int x)
{
    int l = 2*x;
    int r = 2*x+1;
    int si;
    if(l<=heap_size && e[x]<e[l])si = x;
    else si=l;
    if( r<=heap_size && e[si]>e[r])si=r;
    if(si!=x && si<=heap_size)
    {
    //    DBG2(si,x);
    //    DBG2(e[si].val,e[si].id);
     //    DBG2(e[x].val,e[x].id);
         CHANGE(si,x);
    //     DBG2(e[si].val,e[si].id);
    //     DBG2(e[x].val,e[x].id);
         MIN_HEAPIFY(si);
    }
}
void BUIL_HEAP()
{
    for(int i = heap_size/2;i>0;i--)
         MIN_HEAPIFY(i);
}



int main()
{
    LOACL
    FASTIO
    cin>>n>>m;
    REP(i,1,n)
    {
        cin>>a[i]>>b[i]>>c[i];
         
        e[i]=(node){a[i] +b[i] +c[i],1,i};
    }
    heap_size =n ;

    BUIL_HEAP();
    REP(i,1,m)
    {
        cout<<e[1].val<<" ";
        e[1].x++;
        e[1].val = a[e[1].id]*e[1].x*e[1].x+b[e[1].id]*e[1].x+c[e[1].id];
        MIN_HEAPIFY(1);
    }
    //REP(i,1,n) DBG2(e[i].val,e[i].id);


     
    return 0;
}
View Code

 

 
#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
#define FASTIO  ios::sync_with_stdio(false);
#define CLOCK cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";


const int   inf = 987654321;
const int    sz = (int)1e6 + 5;
const int   mod = (int)1e9 + 7;
const int sqrtn = 300; 

//#define add(u,v,w) (e[++tot]=(edge){v,head[u],1},head[u]=tot;) 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl

#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)


#define all(x) x.begin(),x.end()
#define low(x) (x)&(-x)
#define pb push_back
typedef long long ll; 
typedef double dl; 
int n,k;
ll t ,ans;
struct node
{
    ll i,n;
    bool operator< (const node & r) const
    {
        return  n<r.n;
    }
} ;
 
priority_queue< node> q ;
ll l[sz],r[sz],ok[sz],a[sz];
node tmp;
int main()
{
    LOACL
    FASTIO
    
       cin>>n>>k;
       REP(i,1,n)
       {
           cin>>a[i];
           q.push((node){i,a[i]});
           l[i]=i-1;
           r[i]=i+1;
       }

       r[0]=1;
       l[n+1]=n;
       while(k--)
       {
           while(ok[q.top().i]) q.pop();
           tmp = q.top();q.pop();
           if(tmp.n<0)break;
           ans+=tmp.n;

           int x = tmp.i;
           a[x]=a[l[x]]+a[r[x]]-a[x];
           tmp.n=a[x];
           ok[l[x]]=1;ok[r[x]]=1;

           l[x] = l[l[x]];r[l[x]]=x;
           r[x]=r[r[x]];l[r[x]]=x;
           q.push(tmp);
       }
       cout<<ans<<endl;



    CLOCK
    return 0;
}
View Code

 

posted @ 2018-04-07 14:18  pg633  阅读(214)  评论(0编辑  收藏  举报