Ultraviolet A 10271 Chopsticks (linear DP)

Source: Internet
Author: User
Tags bitset

Ultraviolet A 10271 Chopsticks (linear DP)
In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He uses
A set of three chopsticks-one pair, plus an EXTRA long chopstick to get some big food by piercing
It through the food. As you may guess, the length of the two shorter chopsticks shoshould be as close
Possible, but the length of the extra one is not important, as long as it's the longest. To make things
Clearer, for the set of chopsticks with lengths A, B, C (A ≤ B ≤ C), (? B)
2
Is called the "badness"
Of the set.
It's December 2nd, Mr. L's birthday! He invited K people to join his birthday party, and wowould like
To introduce his way of using chopsticks. So, he shocould prepare K + 8 sets of chopsticks (for himself,
His wife, his little son, little daughter, his mother, father, mother-in-law, father-in-law, and K other
Guests). But Mr. L suddenly discovered that his chopsticks are of quite different lengths! He shocould find
A way of composing the K + 8 sets, so that the total badness of all the sets is minimized.
Input
The first line in the input contains a single integer T, indicating the number of test cases (1 ≤ T ≤ 20 ).
Each test case begins with two integers K, N (0 ≤ K ≤ 1000, 3 K + 24 ≤ N ≤ 5000), the number
Guests and the number of chopsticks.
There are N positive integers Li on the next line in non-decreasing order indicating the lengths
The chopsticks (1≤li ≤ 32000 ).
Output
For each test case in the input, print a line containing the minimal total badness of all the sets.
Note: For the sample input, a possible collection of the 9 sets is:
8, 10, 16; 103, 27; 128,129,148, 63, 75; 134,134,139, 88; 157,157,160, 81, 84 ;,;
Sample Input
1
1 40
1 8 10 16 19 22 27 33 36 40 47 52 56 61 63 71 72 75 81 81 84 88 96 98
103 110 113 118 124 128 129 134 134 139 148 157 157 160 162 164
Sample Output

23

To ensure that there is a long chopstick, we can set the minimum cost from the back to the front DP: f [I] [j]: the front I root with the j pair.

 

#include
 
  #include
  
   #include#include
   
    #include
    
     #include
     
      #include
      
       #include
       
        #include
        #include
         
          #include
          
           using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
           
            pil;const int INF = 0x3f3f3f3f;int t,n,k;int a[5500];LL dp[5500][1100];int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&k,&n); k+=8;CLEAR(dp,INF); for(int i=n;i>=1;i--) scanf("%d",&a[i]); dp[0][0]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=k;j++) { dp[i][j]=dp[i-1][j]; if(i-j*3>=0) dp[i][j]=min(dp[i][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1])); } } printf("%lld\n",dp[n][k]); } return 0;}
           
          
         
       
      
     
    
   
  
 

NJUPT 1581

 

 

Description

Mr. A has many chopsticks. To be specific, there should be many roots. Because the chopsticks have different lengths, it is difficult to determine which two are one pair. On this day, Mr. A had K guests and Mr. A left them for dinner. In addition, Mr. A, Mrs. A, and their children, Mr. A, A total of K + 3 people. Each user needs a pair of chopsticks. Mr. A had to clean up the chopsticks. There were N chopsticks in total, with the length of T1, T2, T3 ,......, TN. Now he wants to combine these chopsticks into K + 3 pairs to minimize the sum of squares of the difference in the length of each pair. (Why isn't it the least ?? I am going to ask Mr.)

Input

There are two rows in total. The first two integers are separated by spaces, indicating N, K (1 ≤ N ≤, 0

Output

Only one line. If the K + 3 pairs are not collected, the output value is-1. Otherwise, the minimum value of the sum of the two values of the output length difference is exceeded.

Sample Input

10 1
1 1 2 3 3 3 4 6 10 20

Sample output

5

 

Prompt

First double 1
Second Double 2 3
Third double 3 3
Fourth double 4 6
(1-1) ^ 2 + (2-3) ^ 2 + (3-3) ^ 2 + (4-6) ^ 2 = 5

 

Simplified Version

 

#include
 
  #include
  
   #include#include
   
    #include
    
     #include
     
      #include
      
       #include
       
        #include
        #include
         
          #include
          
           using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
           
            pil;const int INF = 0x3f3f3f3f;int dp[110][55];int L[110];int n,m;int main(){ while(~scanf("%d%d",&n,&m)) { REPF(i,1,n) scanf("%d",&L[i]); sort(L+1,L+n+1); m+=3; CLEAR(dp,INF); dp[0][0]=0; for(int i=2;i<=n;i++) { for(int j=1;j<=m;j++) dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(L[i-1]-L[i])*(L[i-1]-L[i])); } if(dp[n][m]!=INF) printf("%d\n",dp[n][m]); else puts("-1"); } 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.