HDU-5000 Clone (DP)
After eating food from Chernobyl, DRD got a super power: he could clone himself right now! He used this power for several times. He found out that this power was not as perfect as he wanted. For example, some of the cloned objects were tall, while some were short; some of them were fat, and some were thin.
More evidence showed that for two clones A and B, if A was no worse than B in all fields, then B could not survive. More specifically, DRD used a vector v to represent each of his clones. The vector v has n dimensions, representing a clone having N abilities. For the i-th dimension, v[i] is an integer between 0 and T[i], where 0 is the worst and T[i] is the best. For two clones A and B, whose corresponding vectors were p and q, if for 1 <= i <= N, p[i] >= q[i], then B could not survive.
Now, as DRD's friend, ATM wants to know how many clones can survive at most.
More evidence showed that for two clones A and B, if A was no worse than B in all fields, then B could not survive. More specifically, DRD used a vector v to represent each of his clones. The vector v has n dimensions, representing a clone having N abilities. For the i-th dimension, v[i] is an integer between 0 and T[i], where 0 is the worst and T[i] is the best. For two clones A and B, whose corresponding vectors were p and q, if for 1 <= i <= N, p[i] >= q[i], then B could not survive.
Now, as DRD's friend, ATM wants to know how many clones can survive at most.
Input
The first line contains an integer T, denoting the number of the test cases.
For each test case: The first line contains 1 integer N, 1 <= N <= 2000. The second line contains N integers indicating T[1], T[2], ..., T[N]. It guarantees that the sum of T[i] in each test case is no more than 2000 and 1 <= T[i].
For each test case: The first line contains 1 integer N, 1 <= N <= 2000. The second line contains N integers indicating T[1], T[2], ..., T[N]. It guarantees that the sum of T[i] in each test case is no more than 2000 and 1 <= T[i].
Output
For each test case, output an integer representing the answer MOD 10^9 + 7.
Sample Input
2
1
5
2
8 6
Sample Output
1
7
Source
题解: 给你N种属性,每个人的每种属性都在0~T[i]范围内;
参考代码:
当一个人的N属性都小于等于另一个人的属性时,这个人就会被去除,求最多同时存在多少人
容易知道当和为sum/2时最大;
dp[i][j]=(dp[i][j]+dp[i-1][j-k])%MOD; //dp[i][j]表示第i个人时,属性值和为j时最多多少人同时存在;
参考代码:
1 // HDU5000 DP 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define PI acos(-1.0) 5 #define eps 1e-8 6 #define mem(a,b) memset(a,b,sizeof a) 7 typedef long long LL; 8 const int INF=0x3f3f3f3f; 9 const LL inf=0x3f3f3f3f3f3f3f3fLL; 10 typedef pair<int,int> P; 11 #define MOD 1000000007 12 const int maxn=2010; 13 int T,N,sum,t[maxn],dp[maxn][maxn]; 14 15 int main() 16 { 17 ios::sync_with_stdio(false); 18 cin>>T; 19 while(T--) 20 { 21 mem(dp,0); sum=0; 22 cin>>N; 23 for(int i=1;i<=N;++i) cin>>t[i],sum+=t[i]; 24 for(int i=0;i<=t[1];i++) dp[1][i]=1; 25 sum>>=1; 26 for(int i=2;i<=N;i++) 27 { 28 for(int j=0;j<=sum;j++) 29 { 30 for(int k=0;k<=t[i];k++) 31 { 32 if(j<k) continue; 33 dp[i][j]=(dp[i][j]+dp[i-1][j-k])%MOD; 34 } 35 } 36 } 37 cout<<dp[N][sum]<<endl; 38 } 39 return 0; 40 }
Recommend
hujie