The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

Topic: https://nanti.jisuanke.com/t/41420

Ideas: 01 backpack program 

           When a (a∈S ') is the minimum            

           If Sum (S ') - a≤Sum (S-S') established

           Then (∀t∈S ', Sum (S') - t≤Sum (S-S ')) established constant 

           I.e., the current descending order a [i] for the selected minimum value of the stone

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int a[400];
int dp[150001];
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int T;
    scanf("%d",&T);
    int n;
    while(T--)
    {
        scanf("%d",&n);
        int s=0,ans=0;
        for(int i=0;i<n;i++) scanf("%d",&a[i]),s+=a[i];
        sort(a,a+n,cmp);
        memset(dp,0,sizeof dp);
        dp[0]=1;
        for(int i=0;i<n;i++)
            for(int j=s;j>=a[i];j--)
            {
                if(j>=s-j&&j-a[i]<=s-j) ans=(ans+dp[j-a[i]])%mod;
                dp[j]=(dp[j]+dp[j-a[i]])%mod;
            }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/c4Lnn/p/12090711.html