Hdu 3666 Making the Grade

Source: Internet
Author: User

Main TopicA column number is given, which requires modifying certain values so that the final number of columns becomes an ordered sequence, non-increasing or non-decreasing, and the minimum amount of modification is obtained. AnalysisFirst we will find that after the final modification, or the same as the previous number, or the same as the last number, so as to minimize the amount of modification. We first sorted according to the original sequence, determine the size of the element relationship, the corresponding number is p[i]dp[i][j] to consider the first I element, the last element is the sequence of the first J small element of the optimal solution dp[i][j] = MIN (dp[i-1][k]) + ABS (A[i]-a[p[j]), (0 <K<=J) was just beginning to think of the complexity of O (n^3), and later thought that the K value would determine the minimum value at each J cycle.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6 #defineINF 1000000000000007 #defineMAXN 20058 using namespacestd;9 intN;TentypedefLong LongLL; One LL A[MAXN],B[MAXN]; A LL DP[MAXN][MAXN]; - intMain () - { the      while(SCANF ("%d", &n)! =EOF) -     { -          for(intI=1; i<=n; i++) -         { +scanf"%i64d",&a[i]); -b[i]=A[i]; +         } ASort (b +1, B +1+n); at          for(intI=1; i<=n; i++) -              for(intj=1; j<=n; J + +) -dp[i][j]=INF; -          for(intj=1; j<=n; J + +) -         { -dp[1][j]= (a[1]-b[j]); in             if(dp[1][j]<0) -dp[1][j]=-dp[1][j]; to         } +  -          for(intI=2; i<=n; i++) the         { *LL k=dp[i-1][1]; $              for(intj=1; j<=n; J + +)Panax Notoginseng             { -K=min (dp[i-1][j],k); theLL tem= (a[i]-b[j]); +                 if(tem<0) Atem=-tem; theDp[i][j]=min (dp[i][j],k+tem); +                 //printf ("==%i64d\n", Dp[i][j]); -             } $         } $LL minn=INF; -          for(intI=1; i<=n; i++) -minn=min (minn,dp[n][i]); theprintf"%i64d\n", Minn); -     }Wuyi     return 0; the}

Hdu 3666 Making the Grade

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.