Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Minimizing the dot product codechef in c++

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class solution
{
public:
	void dot_product()
	{
		int n;
		cin >> n;

		vector<int> v1(n);
		vector<int> v2(n);

		for (int i = 0; i < n; i++) cin >> v1[i];
		for (int i = 0; i < n; i++) cin >> v2[i];

		sort(v1.begin(), v1.end());
		sort(v2.begin(), v2.end(), greater<int>());

		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			sum += (v1[i] * v2[i]);
		}
		cout << sum << "
";
	}
};

int main()
{
	solution ss;

	int t;
	cin >> t;

	while (t--)
	{
		ss.dot_product();
	}
	
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: whatsup 
Cpp :: point in polygon 
Cpp :: kadane algo 
Cpp :: strcmp in c++ header file 
Cpp :: what is c++ 
Cpp :: c++ optimize big int array 
Cpp :: const in c++ is same as globle in python 
Cpp :: 0-1 knapsack problem implementation of code input array 
Cpp :: cpp module 42 
Cpp :: std::is_standard_layout 
Cpp :: Types of Triangles Based on Angles in c++ 
Cpp :: Opengl GLFW basic window 
Cpp :: gcd multi num 
Cpp :: logisch nicht 
Cpp :: sqrt function in c++ 
Cpp :: start google 
Cpp :: c++ sort numbers by magnitude/absolute value 
Cpp :: Your age doubled is: xx where x is the users age doubled. (print answer with no decimal places) 
Cpp :: distinct numbers cses 
Cpp :: how to get max grade c++ 
Cpp :: converting a string to lowercase inbuld function in cpp 
Cpp :: 28+152+28+38+114 
Cpp :: access the element of vector point2f c++ 
Cpp :: c++ array access operator 
Cpp :: result += a +b in c++ meaning 
Cpp :: c++ insert vector into vector 
Cpp :: c++ ide online 
Cpp :: C++ (ISO) 
Cpp :: cpp full form 
Cpp :: temporary variable ex c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =