HDU 3480 Division (slope optimization)

Source: Internet
Author: User
Divisiontime limit: 10000/5000 MS (Java/others) memory limit: 999999/400000 K (Java/Others) Total submission (s): 2676 accepted submission (s): 1056

Problem descriptionlittle D is really interested in the theorem of sets recently. There's a problem that confused him a long time.
Let t be a set of integers. let the Min be the minimum integer in T and Max be the maximum, then the cost of set T if defined as (max-min) ^ 2. now given an integer set S, we want to find out M subsets S1, S2 ,..., SM of S, such that



And the total cost of each subset is minimal.
Inputthe input contains multiple test cases.
In the first line of the input there's an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers n (≤ 10,000) and M (≤ 5,000 ). n is the number of elements in S (may be duplicated ). M is the number of subsets that we want to get. in the next line, there will be n integers giving set S.

 
Outputfor each test case, output one line containing exactly one integer, the minimal total cost. Take a look at the sample output for format.

 
Sample Input
23 21 2 44 24 7 10 1
 
Sample output
Case 1: 1Case 2: 18HintThe answer will fit into a 32-bit signed integer. 
 
Source2010 ACM-ICPC multi-university training Contest (5) -- host by bjtu
A set containing n elements is divided into M subsets, so that the maximum and least squares of each subset are the same.

Idea: you can think of greedy sorting elements from small to large, and quickly get the minimum cost of J elements in the previous I subsets of DP [I] [J, DP equation DP [I] [J] = DP [I-1] [k] + (A [J]-A [k + 1]); but three loops are required, time complexity is unacceptable, so optimization is required. The slope optimization and quadrilateral inequality are acceptable. I am using the slope optimization just learned. Set k1 <k2 <J. If k1 is better than K2, DP [I-1] [k1] + (A [J]-A [k1 + 1]) ^ 2 <DP [I-1] [k2] + (A [J]-A [k2 + 1]) ^ 2; simplified (DP [I-1] [k1] + A [k1 + 1] ^ 2-(DP [I-1] [k2] + A [k2 + 1] ^ 2 )) /(A [k1 + 1]-A [k2 + 1]) <= 2a [J]; by analogy, y can be obtained: DP [I-1] [k1] + A [k1 + 1] ^ 2; X: A [k1 + 1]; as a result, the slope optimization has the same formula (y1-y2)/(x1-x2) <K; so a monotonic queue can be used to maintain a lower convex function to optimize DP. Slope optimization can be implemented using a rolling array to save some memory.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # define maxn 205 # define maxn 100005 # define mod 100000000 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6typedef long ll; using namespace STD; int n, m, ANS, CNT, TOT, flag; int A [10005], DP [2] [10005]; int Q [10005]; int sqr (int x) {return x * X;} int get (int I, int K) {return DP [I] [k] + A [k + 1] * A [k + 1];} void solve () {int I, j, T, dx1, dy1, dx2, dy2, K1, K2, turn = 0; int head, tail; sort (a + 1, A + n + 1); memset (DP, 0x3f, sizeof (DP); DP [0] [0] = 0; for (I = 1; I <= m; I ++) {head = 0; tail =-1; Q [+ tail] = I-1; For (j = I; j <= N; j ++) {While (Head <tail) // Delete the vertices that no longer update the answer {k1 = Q [head + 1]; k2 = Q [head]; dx1 = A [k1 + 1]-A [k2 + 1]; dy1 = get (turn, K1)-Get (turn, K2 ); if (dy1 <= dx1 * 2 * A [J]) Head ++; else break ;} DP [turn ^ 1] [J] = DP [turn] [Q [head] + sqr (A [J]-A [Q [head] + 1]); while (Head <tail) // maintain the convex curve {dx1 = A [J + 1]-A [Q [tail] + 1]; dy1 = get (turn, J) -Get (turn, Q [tail]); dx2 = A [Q [tail] + 1]-A [Q [tail-1] + 1]; dy2 = get (turn, Q [tail])-Get (turn, Q [tail-1]); If (dy1 * dx2 <= dy2 * dx1) tail --; else break;} Q [++ tail] = J;} turn ^ = 1;} ans = DP [turn] [N];} int main () {int I, j, T, test = 0; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]);} solve (); printf ("case % d: % d \ n", ++ test, ANS);} 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.