poj-1285 Combinations, Once Again(DP)
来源:互联网 发布:照片添加文字软件 编辑:程序博客网 时间:2024/06/05 08:38
题目链接:http://poj.org/problem?id=1285
Description
Input
Output
Sample Input
5 21 2 3 4 52 14 11 2 3 4 20 0
Sample Output
Case 1:105Case 2:6
题意:给一串序列,求一共有多少不同组合。比如样例1,当r=2的时候,有(1,2)(1,3)(1,4)(1,5)(2,3)(2,4)(2,5)(3,4)(3,5)(4,5)一共十种组合。注意当出现重复数字的时候,如果一个组合两个数字都一样,只算一个。比如序列1,1,2 当r=2时有两个解,一个是(1,1),一个是(1,2)
思路:用dp[i][j]表示从i开始(包括i)r=j时的组合数量。 易知dp[i][j]=dp[i+1][j-1]+dp[k][j] k为从i开始第一个不等于i的数(因为dp[k][j]代表不取i,那么[i,k)这个区间内的数都一样,如果取了,就矛盾了。) 注意本题不能把数组排序后删去重复的,因为题目可能询问的很大,那么答案是11112223的可能性很大,去掉将很难处理。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[55];
unsigned long long dp[55][55];
int main()
{
int n,m,r;
int q=1;
while(scanf("%d %d",&n,&m)&&(n&&m))
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
dp[i][0]=1;
dp[n][1]=1;
int k;
for(int i=n-1; i>=1; i--)
{
for(int j=1; j<=n; j++)
{
dp[i][j]=dp[i+1][j-1];
for(k=i+1;k<=n;k++)
if(a[k]!=a[i])
break;
dp[i][j]+=dp[k][j];
}
}
printf("Case %d:\n",q++);
while(m--)
{
scanf("%d",&r);
printf("%llu\n",dp[1][r]);
}
}
return 0;
}
- poj-1285 Combinations, Once Again(DP)
- POJ 1285 - Combinations, Once Again 泛化背包
- POJ 1285 - Combinations, Once Again 泛化背包!
- POJ 1285 Combinations, Once Again 笔记
- 1285:Combinations, Once Again
- codeforces 583B B. Once Again...(dp)
- Codeforces #323 div2. D Once Again... dp LIS
- #Codeforces 323 [div2] D. Once Again 【优化dp】
- Codeforces #323 D. Once Again... (LIS)
- 2 walks Once again
- Code Forces 582 B. Once Again...(LIS)
- CodeForces - 582B Once Again... (LIS变型)好题
- BNUOJ--19139 PvZ once again
- Codeforces 582B Once Again...
- POJ 题目1306 Combinations(排列组合)
- Codeforces Round #323 (Div. 1) B. Once Again... (最长不下降序列_DP)
- 矩阵快速幂好题PvZ once again
- codeforces 583 D. Once Again... (LIS + 贪心)
- Servlet请求多线程访问
- Linux内核分析(二)之简单的进程调度
- 桶排序(BucketSort)
- android 代码连接 wifi
- linux下编译使用lua及cjson
- poj-1285 Combinations, Once Again(DP)
- RSA基本用法原理
- live555学习笔记1
- 基础总结篇之五:BroadcastReceiver应用详解
- three.js教程一场景的基本组件
- 减少资源消耗方法之一:减少状态图片
- yii2 安装问题
- opencv for python学习二之打开视频文件
- sql基础--怎样查询多列数据