bzoj1029 [JSOI2007]建筑抢修

题目

一道贪心的题。
先按照T2排序,能修则修,不能修看能否换掉前面修的,这样绝不会让答案更坏。

手写堆233

#include<bits/stdc++.h>
#define N 150000
using namespace std;
struct building{
    int st;
    int ed;
};building A[N+1],heap[N+1];
int n,tmp,ans,siz;
bool cmp(const building &A,const building &B)
{
    return A.ed<B.ed;
}
char nc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
    int x=0;
    char c=nc();
    while(c<'0'||c>'9')c=nc();
    while(c<='9'&&c>='0')x=x*10+c-'0',c=nc();
    return x;
}
void pushdown(int r,int n)
{
    building v=heap[r];
    int x=r*2;
    while(x<=n)
    {
        if(heap[x].st<heap[x+1].st&&x<n)x++;
        if(heap[x].st<v.st)break;
        heap[r]=heap[x];
        r=x,x=x*2;
    }
    heap[r]=v;
}
void pushup(int r)
{
    building v=heap[r];
    int x=r/2;
    while(x>0)
    {
        if(heap[x].st>v.st)break;
        heap[r]=heap[x];
        r=x,x=x/2;
    }
    heap[r]=v;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("exm.txt","w",stdout);
    n=read();
    for(int i=1;i<=n;i++)A[i].st=read(),A[i].ed=read();
    sort(A+1,A+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        if(tmp+A[i].st<=A[i].ed)
        {
            tmp+=A[i].st;
            ans++;
            heap[++siz]=A[i];
            pushup(siz);
            //cout<<1<<" "<<i<<endl;
        }else{
            building top=heap[1];
            if(A[i].st<top.st)
            {
                heap[1]=A[i];
                pushdown(1,siz);
                tmp=tmp-top.st+A[i].st;
                //cout<<3<<" "<<A[i].st<<endl;
            }
        }
        //cout<<endl<<tmp<<endl;
        //cout<<heap[1].st<<endl;
    }
    cout<<ans;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值