This question yaozhaoguilv
Subject description:
There are two vectors v1 = (x1, x2, x3 , ..., xn), v2 = (y1, y2, y3, ..., yn). Sequence allows any switching component v1 and v2, calculate the inner product of x1 v1 and v2 X2 + Y1 Y2 minimum value xn * yn + ... + a. (1 <= n <= 800 , -1000 <= xi <= 1000, -1000 <= yi <= 1000)
Enter a description:
The first input line of a positive integer n, the number of components of each vector.
The second line of the input vector v1 n integers components, separated by a space between each component.
The third line of input vector v2 n integers components, between each component separated by a space.
Output Description:
An integer, per line, the minimum value of the product of the two vectors.
Sample input:
3
1 3 -5
-2 4 1
Sample output:
-25
Idea: a ascending order, a descending order
Minimum product.
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int n,i,j; 5 while(cin>>n){ 6 long v1[n]; 7 long v2[n]; 8 long sum=0,p,q; 9 for(i=0;i<n;i++){ 10 cin>>v1[i]; 11 } 12 for(i=0;i<n;i++){ 13 cin>>v2[i]; 14 } 15 for(i=0;i<n-1;i++){ 16 for(j=0;j<n-i-1;j++){ 17 if(v1[j]>v1[j+1]){ 18 p=v1[j]; 19 v1[j]=v1[j+1]; 20 v1[j+1]=p; 21 } 22 if(v2[j]<v2[j+1]){ 23 q=v2[j]; 24 v2[j]=v2[j+1]; 25 v2[j+1]=q; 26 } 27 } 28 } 29 for(i=0;i<n;i++){ 30 sum=sum+v1[i]*v2[i]; 31 } 32 cout<<sum<<endl; 33 } 34 return 0; 35 }