[TJOI2013]拯救小矮人[排序+dp]
阿新 • • 發佈:2018-10-20
http per %d tchar getch () git math amp
題意
題目鏈接
分析
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; }
[TJOI2013]拯救小矮人[排序+dp]