NC24614. [USACO 2011 Jan S]Dividing the Gold
描述
输入描述
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer:
输出描述
* Line 1: A single integer that is the smallest difference of two partitions.
* Line 2: A single integer that is the number of ways to split the coins with the minimum difference printed in line 1. Since this number can get quite large, print the remainder when divided by 1,000,000.
示例1
输入:
5 2 1 8 4 16
输出:
1 1
C++14(g++5.4) 解法, 执行用时: 261ms, 内存消耗: 2680K, 提交时间: 2020-03-02 17:08:03
#include<stdio.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) const int p=1000000; int n,x,f[600000],sum; bool g[600000]; int main(){ scanf("%d",&n); f[0]=1;g[0]=1; fo(i,1,n){ scanf("%d",&x); sum+=x; fd(j,sum,x){ g[j]|=g[j-x]; f[j]+=f[j-x]; if (f[j]>=p) f[j]-=p; } } fo(i,(sum+1)/2,sum) if (g[i]){ printf("%d\n%d\n",i-(sum-i),f[i]); return 0; } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 330ms, 内存消耗: 5472K, 提交时间: 2020-03-29 16:38:24
#include<bits/stdc++.h> using namespace std; int n,a[350],pig,s,f[500005]; long long dp[500005]; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i],s+=a[i]; pig=s; s/=2; for(int i=1;i<=n;i++) for(int j=s;j>=a[i];j--) f[j]=max(f[j],f[j-a[i]]+a[i]); cout<<pig-2*f[s]<<endl; dp[0]=1; for(int i=1;i<=n;i++) for(int j=pig;j>=a[i];j--) dp[j]=(dp[j]+dp[j-a[i]])%1000000; cout<<dp[f[s]]<<endl; return 0; }