[DP]Atcoder CODE FESTIVAL 2017 Final (Parallel) D - Zabuton

[DP]Atcoder CODE FESTIVAL 2017 Final (Parallel) D - Zabuton_第1张图片

只要按照H+P排序一下DP就好了,至于为什么按H+P排序自己分类讨论一下就懂了。

代码:

#include
#include
#include
using namespace std;
#define N 5005
#define ll long long
inline char tc(void){
    static char fl[10000],*A=fl,*B=fl;
    return A==B&&(B=(A=fl)+fread(fl,1,10000,stdin),A==B)?EOF:*A++;
}
inline int read(void){
    int a=0;char c;
    while((c=tc())<'0'||c>'9');
    while(c>='0'&&c<='9')a=a*10+c-'0',c=tc();
    return a;
}
struct Zab{
    int h,p,s;
    inline bool operator<(const Zab&a)const{return sint n,ans;
ll f[N][N];
int main(void){
    register int i,j;
    n=read();
    for(i=1;i<=n;++i)
        a[i].h=read(),a[i].p=read(),a[i].s=a[i].h+a[i].p;
    sort(a+1,a+n+1);
    for(i=1;i<=n;++i)
        for(j=n;j;--j){
            if((f[i-1][j-1]^(j==1))&&f[i-1][j-1]<=a[i].h)
                f[i][j]=(f[i][j]==0?f[i-1][j-1]+a[i].p:min(f[i][j],f[i-1][j-1]+a[i].p)),ans=max(ans,(bool)f[i][j]*j);
            if(f[i-1][j])f[i][j]=min(f[i][j],f[i-1][j]);
            if(!f[i][j])f[i][j]=f[i-1][j];
        }
    printf("%d",ans);
    return 0;
}

你可能感兴趣的:(DP,atcoder)