Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
来源:互联网 发布:大数据概念上市公司 编辑:程序博客网 时间:2024/06/14 03:05
题目链接:http://codeforces.com/contest/837/problem/D
题意: 给你n个数,让你任选K个,使得它们乘起来以后结尾的0最多。
解法:
将每个数的因子2和因子5的数量求出来,记作a[i]和b[i]。
答案就是max{ min{Σa[i],Σb[i]} }(a[i],b[i]是选择的那些数)。
暴力dp是f(i,j,k)表示前i个数,选j个,其中包含k个5的情况下,最多能包含多少个2。
转移是f(i,j,k)=max{ {f(t,j-1,k-b[i]}+a[i]}(1<=i<t) , f(i-1,j,k) },时间是O(18 * n^3),但空间存不下。
注意第二维为j时,只会从j-1或者j转移过来,所以可以滚动数组优化。
背包DP里面还有一种不用滚动数组优化的方式,就是逆着枚举,同样可以用到这里。
#include <bits/stdc++.h>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 210;typedef long long LL;const int NN = 206*64;int n, m;LL a[maxn];int dp[maxn][NN];//dp[i][j]表示选i个数其中有j个2最多有多少个5int ans;int main(){ while(~scanf("%d %d", &n,&m)) { for(int i=1; i<=n; i++) scanf("%lld", &a[i]); memset(dp, 0x80, sizeof(dp)); dp[0][0] = 0; for(int i=1; i<=n; i++){ LL x1 = a[i], x2 = a[i]; int cnt2 = 0, cnt5 = 0; while(x1%2==0) x1/=2, cnt2++; while(x2%5==0) x2/=5, cnt5++; for(int k=m; k>=1; k--){ for(int j=cnt2; j<NN; j++){ dp[k][j] = max(dp[k][j], dp[k-1][j-cnt2]+cnt5); } } } ans = 0; for(int i=1; i<NN; i++){ ans = max(ans, min(dp[m][i], i)); } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Educational Codeforces Round 26 D Round Subset
- Codeforces 837D Round Subset【思维+Dp+滚动数组】
- cf Educational Codeforces Round 26 D. Round Subset
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 26 D
- Codeforces-837D Round Subset(dp)
- 【dp】codeforces 837D Round Subset
- codeforces 837D Round Subset (dp)
- Codeforces-837D:Round Subset(DP)
- Educational Codeforces Round 17 D dp
- Educational Codeforces Round 26 D (dp好题)
- Codeforces Round #273 (Div. 2)(D)dp,滚动数组
- Codeforces 837 D Round Subset
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Codeforces 837D Round Subset :随便来个DP
- MySQL--创建时间和更新时间字段
- 8-7
- 140
- 封装日期数据类型的两种实现(algs4)
- 网卡数据的接收
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- linux安装检查ssh
- PE文件格式详解(0)
- 141
- 学习multiple view Geometry个人笔记
- 开源AutoScrollBackLayout列表返回顶部按钮
- AFX_MANAGE_STATE(AfxGetStaticModuleState()) 作用
- 《Python数据分析常用手册》一、NumPy和Pandas篇
- 142