UESTC_ Ferris wheel Cdoj 879

Source: Internet
Author: User

One day, the winter horse was Chunch and snow vegetables to go to the amusement park to play together.

After the baptism of various roller coaster, the three people decided to take the Ferris wheel to rest.

This is a huge Ferris wheel, each compartment can sit as many people as possible. Now, waiting to sit on the Ferris wheelNindividuals (including their3People), Ferris wheel andmA car can sit on a person. Everyone has their own degree of obesity, for some reason, fat and skinny sitting in the same car will have a certain contradiction, the value of this contradiction is(mAX−mIN)2 , where maX is the obese degree of the fattest person in the current compartment,mIN is the obese degree of the most thin person.

Nosy Spring, of course, do not want to because of this little thing and make everyone's trip unpleasant, so he decided to help you arrange how to sit to make the total contradiction value of the smallest, I hope you can help him find this minimum contradiction value

Input

First act two integersn,m , representing the number of persons and carriages respectively.(3≤n≤10000,1≤m≤)

Second actNAn integer,Wi Represents the first I obesity degree of individuals. (0≤wi≤1000000)

Output

Each set of data, output an integer, which is the minimum value of the contradiction. (The answer is guaranteed to be less than 2)

Sample Input and output
Sample Input Sample Output
4 24 7) 10 1
18

Problem Solving Report

Slope optimization is required and the space is complex enough to be optimized to O (n) (n or M). Forget it, it's not MN anyway

We'll update it tomorrow. The first code, the space complexity is O (MN)

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;Const intMAXN = the+ the;inta[maxn*2],n,m,q[maxn*2],dp[maxn*2][maxn];inlineDoubleSlope (intQintWintj) {    if(a[q+1] = = a[w+1])return1e233; return 1.0* (dp[q][j-1]+a[q+1]*a[q+1]-dp[w][j-1]-a[w+1]*a[w+1])/ (2* (a[q+1]-a[w+1])) ;}intMainintargcChar*argv[]) {scanf ("%d%d",&n,&m);  for(inti =1; I <= N; ++i) scanf ("%d",&A[i]); Sort (a+1, A +1+N); A[n+1] =0;//Just to Avoild Some speical situation-_=   for(inti =1; I <= N; + + i) dp[i][1] = (a[i]-a[1]) * (a[i]-a[1]);  for(intj =2; J <= M; ++j) {intFront =0, rear =0; Q[rear++] =1;  for(inti =2; I <= N; ++i) { while(Rear-front >1&& Slope (q[front],q[front+1],J) <=A[i]) front++; DP[I][J]= dp[q[front]][j-1] + (a[i]-a[q[front]+1]) * (a[i]-a[q[front]+1]);  while(Rear-front >1&& Slope (q[rear-2],q[rear-1],J) >= Slope (q[rear-1],i,j)) rear--; Q[rear++] =i; }} printf ("%d\n", Dp[n][m]); return 0;}

UESTC_ Ferris wheel Cdoj 879

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.