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.
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].
2 1 5 2 8 6
1 7
题意:有n种属性,每种属性的数值可以是0-T[i],当一个人属性全部小于等于另一个人的属性时,小的那个人会被淘汰,问最多同时存在多少人
思路:首先如果两个人的属性和相同的话,那么这两个人一定能共存(有多就有少,或者全相同),并且共存的最多一定是所有人的属性值相同的时候,设所有属性的值之和是ans,那么当sum=0和sum=ans的时候人数是相同的(对称的),所有最多的时候应该是sum=ans/2的时候,那么题目就完全转变了
变为了从n个区间中选择n个数,使得n个数的和为ans/2,这样的话dp就可以解决了,dp[i][j] i表示选到第i个数 j表示所选的所有数的和
ac代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const int mod=1e9+7; int a[2005]; int dp[2005][2005]; int main() { int t,n; cin>>t; while(t--) { cin>>n; int sum=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } memset(dp,0,sizeof(dp)); for(int i=0;i<=a[1];i++) dp[1][i]=1; for(int i=2;i<=n;i++) { for(int j=0;j<=sum;j++) { for(int k=0;k<=j&&k<=a[i];k++) { dp[i][j]=(dp[i][j]+dp[i-1][j-k])%mod; } } } printf("%d\n",dp[n][sum/2]); } return 0; }