CF837D Round Subset 动态规划

开始的时候数据范围算错了~

我以为整个序列 2 和 5 的个数都不超过 70 ~

一个非常水的 dp 

code: 

#include <bits/stdc++.h>  
#define M 75   
#define N 201  
#define LL long long 
using namespace std;  
void setIO(string s) 
{
    string in=s+".in"; 
    string out=s+".out"; 
    freopen(in.c_str(),"r",stdin);  
    freopen(out.c_str(),"w",stdout);   
}
int f[N][6800];    
struct node 
{
    int t2,t5;         
}t[N];                      
int main() 
{ 
    // setIO("dynamic-programming");    
    int i,j,n,k;
    scanf("%d%d",&n,&k);         
    memset(f,-0x3f,sizeof(f));   
    f[0][0]=0;                 
    for(i=1;i<=n;++i) 
    {
        LL x; 
        scanf("%lld",&x);               
        while(x%2==0) ++t[i].t2,x/=2;  
        while(x%5==0) ++t[i].t5,x/=5;                          
    }   
    for(i=1;i<=n;++i) 
    {
        for(j=i;j>=1;--j) 
        {
            for(int p=6700;p>=t[i].t5;--p) 
            {
                f[j][p]=max(f[j][p], f[j-1][p-t[i].t5]+t[i].t2);     
            }
        }
    }   
    int best=0; 
    for(i=1;i<=k;++i) 
    {
        for(j=1;j<=6700;++j) 
        {
            best=max(best,min(j, f[i][j]));          
        }
    }    
    printf("%d\n",best);   
    return 0; 
}

  

猜你喜欢

转载自www.cnblogs.com/guangheli/p/11758593.html