Category : optimization

How to optimise this code to reduce time complexity?It works but some test cases are getting time limit exceed error. Please help me to reduce the time complexity ? int minimumCost(int n,int m,vector<vector<int>>teams){ sort(teams.begin(),teams.end(),[&](const vector<int>& a,const vector<int>& b)-> bool{ if(a[1]==b[1]){ return a[0]<b[0]; } return a[1]<b[1]; }); long long dp[m]; for(int i=0;i<teams.size();i++){ bool f=false; int g=teams[i][1]-teams[i][0]+1; ..

Read more

Problme: I won’t get the result that I get from Matlab implementation, I am not sure pow_pos(norm(x(:, 1) – start’), 2) I have converted correctly, here is the converted code Variable::t x_0 = Var::vstack(x->index(0, 0), x->index(1, 0), x->index(2, 0)); M->constraint(Expr::vstack(t_tmp->index(0), Expr::sub(x_0, start_)), Domain::inQCone()); M->constraint(Expr::hstack(t->index(0), 1, t_tmp->index(0)), Domain::inPPowerCone(1.0/2)); Here is the original code I wrote in ..

Read more

class classN { public: bool operator== (const classN &o) const {//some logic here}; bool operator!= (const classN &o) const {return !(*this == o);}; }; I see this as the best way to write overloads of operator== and operator!=, as I need to change code only once. however, I’ve seen in different projects people writing actual ..

Read more

I have a set of constraints in the following way, yet could not formulate it properly. In the following way, I could take the norm, but how can we get its power? Model::t M = new Model(); auto start_ = new_array_ptr<double, 1>({-4, 0, 2}); auto x_0 = M->variable(new_array_ptr<int,1>({3, 1}), Domain::unbounded()); M->constraint(Expr::vstack(t->index(0), Expr::sub(x_0, start_)), Domain::inQCone()); If ..

Read more

I am having a strange error still could not figure it out. #include <iostream> #include "fusion.h" using namespace mosek::fusion; using namespace monty; int main(int argc, char ** argv) { int number_of_steps = 7; int lambda1 = 1000; int lambda2 = 1000; int dim_space = 3; auto A = new_array_ptr<double, 2>({ {-0.1504 , 0.5675 , -0.7252}, ..

Read more

Main.cpp: #include <iostream> #include "Reals.h" #include "Timer.h" int main() { std::cout.precision(8); real<> vals[] { 0.f, -0.f, .0035f, -0.0035f, 1.f, -1.f, -2374.3f, -2374.2999f, 8452.9f, 8452.901f, std::numeric_limits<float>::min(), -std::numeric_limits<float>::min(), std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), INFINITY, -INFINITY, NAN }; float fals[] { 0.f, -0.f, .0035f, -0.0035f, 1.f, -1.f, -2374.3f, -2374.2999f, 8452.9f, 8452.901f, std::numeric_limits<float>::min(), -std::numeric_limits<float>::min(), std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), INFINITY, -INFINITY, NAN }; std::cout << ..

Read more

I’m trying to make some kind of Hamiltonian path implementation. There is an error in the nearest neighbor finder function, which I could not solve. According to the following figure node2’s index should be printed while previous node was node0 and current node is node1, but the function returns the node0. returnNearestNeighbor() function: Node* returnNearestNeighbor(Node& ..

Read more

The following code: #include <vector> extern std::vector<int> rng; int main() { auto is_even=[](int x){return x%2==0;}; int res=0; for(int x:rng){ if(is_even(x))res+=x; } return res; } is optimized by GCC 11.1 (link to Godbolt) in a very different way than: #include <vector> extern std::vector<int> rng; int main() { int res=0; for(int x:rng){ if(x%2==0)res+=x; } return res; } ..

Read more