是这题数据水还是。。。(数据怎么知道我人数有没有超一半啊)
f[ i ][ j ] 前 i 件物品中分数不超过 j 的最大分数
j 记录分数。。。
然后降一维01背包求解
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<cstring> #include<queue> using namespace std; typedef long long ll; inline int read() { int ans=0; char last=‘ ‘,ch=getchar(); while(ch<‘0‘||ch>‘9‘) last=ch,ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘) ans=ans*10+ch-‘0‘,ch=getchar(); if(last==‘-‘) ans=-ans; return ans; } int n; int s[105]; int f[5005]; int ave=0,ans=0; int main() { n=read(); for(int i=1;i<=n;i++) s[i]=read(),ave+=s[i]; ave=ave/2; for(int i=1;i<=n;i++) for(int j=ave;j>=s[i];j--){ f[j]=max(f[j],f[j-s[i]]+s[i]); ans=max(ans,f[j]); } printf("%d\n",ans); return 0; }
原文:https://www.cnblogs.com/xiaoyezi-wink/p/11959216.html