一道贪心的题。
先按照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;
}