【dp】数字编码

在这里插入图片描述


思路:

类似模拟¿
就题目怎么说就怎么dp


C o d e Code Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,two2;
int dp[30005],a[30005],two[30005];
int main ()
{
    
    
	freopen ("coding.in","r",stdin);
	freopen ("coding.out","w",stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
    
      
	    scanf("%d", &a[i]);
	    int jl = 0;
	    while (a[i] != 0)//转2进制
	    {
    
    
	  	  a[i] >>= 1;
	  	  jl++;
	  	  two[i] = jl;
	  	}
    }
    for (int i = n; i >= 1; --i)//dp
	{
    
    
		dp[i] = dp[i + 1] + two[i] + 12;
		two2 = two[i];
		for (int j = i + 2; j <= min (i + 255,n + 1); ++j)
		{
    
    
			two2 = max (two2,two[j - 1]);
			dp[i] = min (dp[i],dp[j] + 12 + two2 * (j - i));
		}
	}    
	  printf("%d",dp[1]);
}

猜你喜欢

转载自blog.csdn.net/hunkwu/article/details/108147096