标签:style blog class code c tar
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3682
The 2012 Europe Cup was over and Spain won the Championship. The fans of Spain want to hold some activities to celebrate. But there is a question. Some of them are the fans of F.C Barcelona
and the others are Real Madrid. It is well-known that the relation of these two clubs is very bad. So they may have some disharmony events when the fans of the two clubs celebrate together.
There are s1 fans of Barcelona and s2 fans of Madrid (0 ≤ s1, s2 ≤ 100000). The government provided n (2 ≤ n ≤ 200) squares and each square can contain ki fans. Only if each square
is all Barcelona‘s fans or Madrid‘s fans or the number of two clubs is equal exactly, it is harmony.
Your task is calculating the sum of ways that the celebration is harmony.
There are multiple test cases(no more than 50). For each test case:
The first line contains two integers s1 and s2 (0 ≤ s1, s2 ≤ 100000), the number of fans of Barcelona and Madrid. The second line contains one integer n (2 ≤ n ≤ 200), the number of squares. The third line contains n integers,
the ith integer is ki (1 ≤ i ≤ n). We promise that the sum of ki is equal to the sum of s1 and s2.The sum will modulo 1000000007.
In each test case, output the sum of ways.
2 1 2 2 1
2
http://en.wikipedia.org/wiki/FC_Barcelona
http://en.wikipedia.org/wiki/Real_Madrid
题目意思:
有s1个人支持球队1,s2个人支持球队2,有n个房子,每个房子可容纳的人数为ki, 满足sum(ki)=s1+s2。每个房子要么全部容纳支持球队1的人,要么全部容纳支持球队2的人,要么一半是支持球队1的一半是支持球队2的,求分配的方案数。
解题思路:
dp.
dp[i][j]:表示分完前i个房子后支持球队1的还剩下j个人的方案总数。因为分完前i个房子后,后面需要分的人的总数是确定的,知道支持球队1的人数后,剩下的就是支持球队2的。
显然对于每个房子要么全部分配给球队1,要么全部分配给球队2,如果是偶数的话还可以两支球队各分一半。
dp[i][j]=max(dp[i-1][j+save[i]],dp[i-1][j],dp[i-1][j+save[i]/2).
代码:
#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #define ll long long #include<cstdlib> using namespace std; #define M 1000000007 #define Maxn 210000 ll dp[2][Maxn]; int n,save[220],sum[220]; ll s1,s2; int zong; int main() { while(~scanf("%lld%lld",&s1,&s2)) { scanf("%d",&n); sum[0]=0; zong=0; for(int i=1;i<=n;i++) { scanf("%d",&save[i]); zong+=save[i]; sum[i]=sum[i-1]+save[i]; } if(s1==0||s2==0) { printf("1\n"); continue; } memset(dp,0,sizeof(dp)); dp[0][s1]=1; //第一个人剩下s1 int cur=0; for(int i=1;i<=n;i++) { cur=cur^1; memset(dp[cur],0,sizeof(dp[cur])); int lim=zong-sum[i]; //if(save[i]&1) { for(int j=lim;j>=0;j--) { dp[cur][j]=(dp[cur][j]+dp[cur^1][j+save[i]])%M; dp[cur][j]=(dp[cur][j]+dp[cur^1][j])%M; } } if(!(save[i]&1)) { for(int j=lim;j>=0;j--) { dp[cur][j]=(dp[cur][j]+dp[cur^1][j+save[i]/2])%M; } } /*for(int j=lim;j>=0;j--) { printf("i:%d j:%d %lld\n",i,j,dp[cur][j]); system("pause"); }*/ } printf("%lld\n",dp[cur][0]); } return 0; } /* 2 2 3 1 1 2 */
[dp] zoj 3682 E - Cup 3,布布扣,bubuko.com
标签:style blog class code c tar
原文地址:http://blog.csdn.net/cc_again/article/details/25689815