HDU4570 ---- Multi-bit Trie ---- simple DP

Source: Internet
Author: User

Question meaning:

N count

You can divide it into multiple segments. The length of each segment cannot exceed 20.

Yes, sum (ai * (2 ^ bi) is the smallest. ai is the first number in each segment, and bi is the length.

Solution:

Set dp [I] = min (dp [I], dp [j] + a [I] * 2 ^ (j-I), 1 <= I <= n, I + 1 <= j <= min (I + 20, n + 1)

Dp [I] indicates the value starting with I

Finally, we will compare dp [1] with a comparison into n segments.

There are a lot of code on the Internet. I wrote an iteration, which is faster than the memory-based search.

 

#include<cstdio>   #include<cstring>   #include<cmath>   #include<algorithm>   #include<iostream>   using namespace std;    #define ULL long long     const int maxn = 65;    ULL dp[maxn];  ULL a[maxn];  int n;    int main()  {      int t;      scanf("%d",&t);      while(t--)      {          scanf("%d",&n);          ULL ans=0;          for(int i=1;i<=n;i++)          {              cin>>a[i];              ans += a[i]*2;          }            dp[n] = a[n]*2;          dp[n+1] = 0;          for(int i=n-1;i>=1;i--)          {              dp[i] = (1ULL<<62-1);              for(int j=i+1;j<=min(i+20,n+1);j++)              {                  dp[i] = min(dp[i],dp[j]+a[i]*(1<<(j-i)));              }          }            cout<<min(ans,dp[1])<<endl;      }      return 0;  }  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.