Stone game(01背包)

本题运用01背包,一维滚动数组,主要是要排个序,找到你选择的s'中的最小值

CSL loves stone games. He has nn stones; each has a weight a_iai​. CSL wants to get some stones. The rule is that the pile he gets should have a higher or equal total weight than the rest; however if he removes any stone in the pile he gets, the total weight of the pile he gets will be no higher than the rest. It's so easy for CSL, because CSL is a talented stone-gamer, who can win almost every stone game! So he wants to know the number of possible plans. The answer may be large, so you should tell CSL the answer modulo 10^9 + 7109+7.

Formerly, you are given a labelled multiset S=\{a_1,a_2,\ldots,a_n\}S={a1​,a2​,…,an​}, find the number of subsets of SS: S'=\{a_{i_1}, a_{i_2}, \ldots, a_{i_k} \}S′={ai1​​,ai2​​,…,aik​​}, such that

\left(Sum(S') \ge Sum(S-S') \right) \land \left(\forall t \in S', Sum(S') - t \le Sum(S-S') \right) .(Sum(S′)≥Sum(S−S′))∧(∀t∈S′,Sum(S′)−t≤Sum(S−S′)).

 

InputFile

The first line an integer TT (1 \leq T \leq 10)1≤T≤10), which is the number of cases.

For each test case, the first line is an integer nn (1 \leq n \leq 3001≤n≤300), which means the number of stones. The second line are nn space-separated integers a_1,a_2,\ldots,a_na1​,a2​,…,an​ (1 \leq a_i \leq 5001≤ai​≤500).

OutputFile

For each case, a line of only one integer tt --- the number of possible plans. If the answer is too large, please output the answer modulo 10^9 + 7109+7.

样例输入复制

2
3
1 2 2
3
1 2 4

样例输出复制

2
1

样例解释

In example 1, CSL can choose the stone 1 and 2 or stone 1 and 3. 

 In example 2, CSL can choose the stone 3.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define mod 1000000007
int a[400];
int dp[150010];
bool cmp(int c,int d)
{
	return c>d;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n;
		scanf("%d",&n);
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		sort(a+1,a+1+n,cmp);
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=sum;j>=a[i];j--)
			{
				 if(j>=sum-j&&j-a[i]<=sum-j)
				 {
				 	ans+=dp[j-a[i]];
				 	ans=ans%mod;
				 }
				 dp[j]=(dp[j-a[i]]+dp[j])%mod;
			}
		}
		 printf("%d\n",ans);
	}
	return 0; 
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值