Zju1234 chopsticks-Dynamic Planning

Source: Internet
Author: User

Description:

One person invented a new method of using chopsticks. Each time three chopsticks were used, the chopsticks were of different lengths (A <= B <= C ), specify the "not moderate" of the three chopsticks as indicated by (A-B) ^ 2. Now there are n chopsticks with different lengths. I want to know the sum of the minimum and moderate values of M chopsticks. Input according to the chopsticks length in non-descending order.

Analysis:

ThisDynamic PlanningThe deformation is strange. The length of the three chopsticks, the longest one, is useless, but must exist.

Array a [I] indicates the length of the I-root chopsticks.Non-ascendingThe subscript starts from 1. UseStatus f [I] [J] indicates that j pairs are selected from the I-root chopsticks, and the minimum deviation is moderate..

It can be proved that if you choose the [I] root chopsticks as the shortest, then the short chopsticks must be a [I-1]. Therefore, note that bad (I) indicates that the I-root chopsticks are obtained as the shortest result.

Apparently, the initial status is:

F [I] [0] = 0; F [3] [1] = bad (3 ).

State transition equationF [I] [J] = min {f [I-1] [J], F [I-2] [J-1] + bad (I )}.

Note that f [I] [J] is invalid.

/*ZJU1234 Chopsticks*/#include 
 
  #include 
  
   #define N 5005#define K 1005#define INF 99999999#define clr(a) memset(a,0,sizeof(a))#define MIN(a,b) ((a)>(b)?(b):(a))#define POW(a) ((a)*(a))int n,m;int a[N];int f[N][K];int bad(int i){    return POW(a[i]-a[i-1]);}int main(){    int i,j,k,T;    int min;        scanf("%d",&T);    while(T--){        //init        clr(a);        clr(f);        min=INF;                //input        scanf("%d%d",&k,&n);        m=k+8;        for(i=n;i>=1;i--)            scanf("%d",&a[i]);                //DP        f[3][1]=bad(3);        for(i=4;i<=n;i++){            k=MIN(i/3,m);            for(j=1;j<=k;j++){                if(j*3==i) f[i][j]=INF;                else f[i][j]=f[i-1][j];                f[i][j]=MIN(f[i][j],f[i-2][j-1]+bad(i));                if(j==m) min=MIN(min,f[i][j]);            }        }                //output        printf("%d/n",min);            }        return 0;}
   

   

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.