Driving (a1 * b1 + a1 * b2 + .. a1 * bn + a2 * b1 +... an * bn = (a1 + .. an) (b1 + .. bn ))

Source: Internet
Author: User

Driving
(Bicycle. pas/cpp)
Description
Riding on a bicycle, let the breeze chase his clothes and inadvertently capture a charming heart, the sun does not seem to shine at this time, this is the pride of the data center-jiande!
This is a scene that occurs at the entrance of the appendix every day. In order to be able to see different scenery and capture more hearts every day, jiande plans to develop n lines. For the sake of simplicity, we think of the world as a plane Cartesian coordinate system, while jiande's Fujian Normal University High School is the origin. Because jiande cannot go too far, the destination of each route is restricted to a rectangle (x, y) in the upper right corner and (-x,-y) in the lower left corner.
Each jiande will go straight from the origin point to the destination. Apparently, he walked through a vector, which is called the route vector by mathematical control. In order to better plan the line, he defined a boring value for each line, that is, the point and area of this route vector and the vectors of all other lines before the embedding, y1), (x2, y2), their dot and area are x1 * x2 + y1 * y2 ]. Jiande hopes to reasonably select the destination so that all lines have the least and least boring value.
Input Format
The first line is a positive integer n, indicating that jiande intends to develop n travel routes.
In the next n rows, each line contains two integers x and y, which describe a rectangle that limits the destination.
Output Format
One integer in a row, that is, the smallest boring value, with two decimal places reserved.
Sample Input
2
1 2
2 1
Sample output
-4.00
Data scope and conventions
For 10% of the data, ensure that 0 <n <= 5, 0 <x, y <= 5.
For 30% of data, ensure that 0 <n <= 20, 0 <x, y <= 100.
For 100% of data, ensure that 0 <n <= 200, 0 <x, y <=.

First, the result of multiplying n numbers and m numbers is the sum of n numbers and m numbers.
And x and y do not affect each other.
Then the result of the formula-multiply by two or the 2-intersection is f (n) = (x1 +... + xn) ^ 2 + x1 ^ 2 +... xn ^ 2)/2.
E certificate f (n) = (x1 +... + xn-1) * xn + f (n-1)
Therefore, if xn is either the maximum or the minimum value, f (n) can have the Maximum/minimum value.
Because I don't know (the positive and negative values of x1 +. x (n-1) can only be guessed (considering both the maximum and the minimum)
Return to the original formula. Obviously, if f (n) has a minimum value, it must have another (x1 +... + xn) with a minimum value.
So it's about grouping backpacks ......

[Cpp]
# Include <cstdio>
# Include <cstdlib>
# Include <iostream>
# Include <cstring>
# Include <cmath>
# Include <functional>
# Include <algorithm>
Using namespace std;
# Define MAXN (200 + 10)
# Define MAXV (40000 + 100) * 2)
# Define MAXX (200 + 10)
# Define f (I, j) f [(I)] [(j) + 20000]
Int n;
Long ans = 0;
Bool f [MAXN] [MAXV];
Int x [MAXN];
Int y [MAXN];
Int sqr (int x)
{
Return x * x;
}
Int main ()
{
Freopen ("bicycle. in", "r", stdin );
Freopen ("bicycle. out", "w", stdout );
Cin> n;
For (int I = 1; I <= n; I ++)
{
Cin> x [I]> y [I];
Ans-= (long) (sqr (x [I]) + sqr (y [I]);
}
Memset (f, 0, sizeof (f ));
F (0, 0) = 1;

For (int I = 1; I <= n; I ++)
For (int j =-I * 200; j <= I * 200; j ++)
F (I, j) = f (I-1, j-x [I]) | f (I-1, j + x [I]);

Int j = 0;

While (! F (n, j )&&! F (n,-j) j ++;
Ans + = (long) j * j;

// Cout <j <endl;

/* Int j = 0;
For (int I = 0; I <= n; I ++)
{
For (int j =-10; j <= 10; j ++)
Cout <f (I, j );
Cout <endl;
}
*/
Memset (f, 0, sizeof (f ));
F (0, 0) = 1;

For (int I = 1; I <= n; I ++)
For (int j =-I * 200; j <= I * 200; j ++)
F (I, j) = f (I-1, j-y [I]) | f (I-1, j + y [I]);

J = 0;
While (! F (n, j )&&! F (n,-j) j ++;

Ans + = (long) j * j;

Cout. setf (ios: fixed );
Cout. precision (2 );
Cout <double (ans)/2 <endl;


// While (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.