感觉动规这种东西怎么也学不会了.....
分析
贪心+动规
http://blog.csdn.net/chen1352/article/details/52234046
说的已经很好了。
代码
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=5000+5; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } int n,ans; int f[maxn][maxn]; struct node { int v,w; bool operator < (const node &j) const { return w>j.w; } }a[maxn]; int main() { freopen("value.in","r",stdin); freopen("value.out","w",stdout); n=read(); for(int i=1;i<=n;i++) a[i].v=read(),a[i].w=read(); sort(a+1,a+n+1); //memset(f,-0x3f,sizeof(f)); //for(int i=1;i<=n;i++) f[i][0]=0; for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) { f[i][j]=max(f[i-1][j],f[i-1][j-1]+a[i].v-a[i].w*(j-1)); ans=max(ans,f[i][j]); } printf("%d ",ans); }