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

Posted c4lnn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game相关的知识,希望对你有一定的参考价值。

题目:https://nanti.jisuanke.com/t/41420

思路:当a(a∈S′)为最小值         

           如果Sum(S′)−a≤Sum(S−S′)成立

           那么(∀t∈S′,Sum(S′)−t≤Sum(S−S′))恒成立 

           先算01背包方案数

           再从小到大排序进行退背包

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

以上是关于The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game的主要内容,如果未能解决你的问题,请参考以下文章

The Preliminary Contest for ICPC Asia Yinchuan 2019

The Preliminary Contest for ICPC Asia Shenyang 2019

The Preliminary Contest for ICPC Asia Shanghai 2019

The Preliminary Contest for ICPC Asia Shanghai 2019

The Preliminary Contest for ICPC Asia Xuzhou 2019

The Preliminary Contest for ICPC Asia Nanchang 2019