Stone game(ICPC 上海站)

Stone game

題目描述:

CSL loves stone games. He has nnn stones; each has a weight aia_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 109+710^9 + 7109+7.Formerly, you are given a labelled multiset S={a1,a2,…,an}S={a_1,a_2,\ldots,a_n}S={a1​,a2​,…,an​}, find the number of subsets of SSS: S′={ai1,ai2,…,aik}S’={a_{i_1}, a_{i_2}, \ldots, a_{i_k} }S′={ai1​​,ai2​​,…,aik​​}, such that (Sum(S′)≥Sum(S−S′))∧(∀t∈S′,Sum(S′)−t≤Sum(S−S′)).\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′)).

輸入:

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

輸出:

For each case, a line of only one integer ttt — the number of possible plans. If the answer is too large, please output the answer modulo 109+710^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.

code:

一直在想如何保存,想了許久,現在來補一下題目
我們考慮這道題,意思是,一堆石頭,一個人要從中取出一堆石頭,使取出來的石頭比剩下來的石頭的質量要重,並且從中已經取出來的石頭堆裏取出任意一顆石頭,這樣的一堆石頭比原來剩下的石頭的質量要輕,那麼只要最輕的那顆石頭滿足,那麼就都滿足了

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=355;
const long long mod=1e9+7;
long long dp[maxn*505];
int a[maxn];
bool cmp(int x,int y)
{
 return x>y;
}
int main()
{
 int ttt;
 scanf("%d",&ttt);
 while(ttt--){
  int n;
  scanf("%d",&n);
  memset(dp,0,sizeof(dp));
  int sum=0;
  for(int i=1;i<=n;i++){
   scanf("%d",&a[i]);
   sum+=a[i];
  }
  long long ans=0;
  dp[0]=1;
  sort(a+1,a+n+1,cmp);
  for(int i=1;i<=n;i++){
   for(int j=sum;j>=a[i];j--){
    dp[j]+=dp[j-a[i]];
    dp[j]=dp[j]%mod;
    if((j>=sum-j)&&(j-a[i]<=sum-j)){
     ans+=dp[j-a[i]];
     ans=ans%mod;
    }
   }
  }
  printf("%lld\n",ans);
 }
 return 0;
 } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章