[dp] zoj 3682 E - Cup 3
来源:互联网 发布:中国蓝tv网络直播 编辑:程序博客网 时间:2024/04/30 10:44
题目链接:
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.
Input
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.
Output
In each test case, output the sum of ways.
Sample Input
2 122 1
Sample Output
2
Reference
http://en.wikipedia.org/wiki/FC_Barcelona
http://en.wikipedia.org/wiki/Real_Madrid
Author: ZHOU, Xiao
Contest: ZOJ Monthly, January 2013
题目意思:
有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 210000ll 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 231 1 2*/
- [dp] zoj 3682 E - Cup 3
- ZOJ-3682-E - Cup 3【dp】
- ZOJ-3682 E - Cup 3 DP+01背包
- ZOJ 3682 E - Cup 3
- ZOJ 3682 E - Cup 3
- [ZOJ 3682] E - Cup 3 (背包DP计数 + 滚动数组)
- ZOJ 3681 E - Cup 2
- ZOJ 3681 E - Cup 2
- ZOJ-3681 E - Cup 2 dfs
- [记忆化搜索] zoj 3681 E - Cup 2
- 【dp】VK Cup 2012 Qualification Round 1 ——E
- VK cup 2017 E. Singer House (奇妙的dp)
- ZOJ 3681E - Cup 2(记忆化dfs)不好读
- Canada Cup 2016 E
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) E. Levels and Regions(斜率优化dp) ★ ★ ★
- VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP
- VK Cup 2012 Finals E
- [线段树 杂题] Codeforces 806E VK Cup 2017 Round 3 E. Blog Post Rating
- 百度网盘之android4.2源码下载
- The connection to adb is down, and a severe error has occured.
- Android Application的相关技术
- 将事件添加到系统日历中,并且添加到提醒事项中
- 如何在WINDOWS(XP)下使用copSSH配置GIT服务器+TortiseGIT客户端
- [dp] zoj 3682 E - Cup 3
- jquery easyui datagrid 分页 详解
- mysql使用名利单独导出存储过程文件
- 32位80x86汇编语言ptr指令学习笔记
- iOS开发者企业计划
- 创建二叉树
- 设计模式学习01—单例模式
- 阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql
- 创意无限!一组网页边栏过渡动画【附源码下载】