[TJOI2013]拯救小矮人[排序+dp]

时间:2023-03-09 08:37:30
[TJOI2013]拯救小矮人[排序+dp]

题意

题目链接

分析

Imagine的完美回答

重点大概是证明我们选出要救的小矮人一定可以根据 \(a_i+b_i\) 的大小进行排序救出。

注意这里关注的对象是可以保留的高度,所以我们的dp值才会表示成最少减少的高度。

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=2e3 + 7;
const LL inf=1e15;
int n,ans;
LL h,sum;
LL f[N][N];
struct data{
LL a,b;
bool operator <(const data &rhs)const{
return a+b<rhs.a+rhs.b;
}
}t[N];
int main(){
n=gi();
rep(i,1,n) t[i].a=gi(),t[i].b=gi(),sum+=t[i].a;
h=gi();
sort(t+1,t+1+n);
rep(i,0,N-1)rep(j,0,N-1) f[i][j]=inf;
f[0][0]=0;
rep(i,0,n)
rep(j,0,n){
if(sum-f[i][j]+t[i+1].b>=h) Min(f[i+1][j+1],f[i][j]+t[i+1].a);
Min(f[i+1][j],f[i][j]);
}
rep(j,0,n) if(f[n][j]!=inf) Max(ans,j);
printf("%d\n",ans);
return 0;
}