From 2c60625a825daf9d16a0ef76b38926894250cd12 Mon Sep 17 00:00:00 2001 From: sakd23 <89689939+sakd23@users.noreply.github.com> Date: Fri, 22 Oct 2021 14:20:49 +0530 Subject: [PATCH] another solution of hassan and trip --- Dynamic Programming-1/Hassan_and_Trip.cpp | 100 ++++++++++++---------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/Dynamic Programming-1/Hassan_and_Trip.cpp b/Dynamic Programming-1/Hassan_and_Trip.cpp index da9d812..f76ffb4 100644 --- a/Dynamic Programming-1/Hassan_and_Trip.cpp +++ b/Dynamic Programming-1/Hassan_and_Trip.cpp @@ -25,50 +25,60 @@ Sample Output -#include -#include -#include +#include using namespace std; -double distanc(double *x, double *y, int i, int j) -{ - double difference_x=x[i]-x[j]; - double difference_y=y[i]-y[j]; - double ans= sqrt((difference_x*difference_x)+(difference_y*difference_y)); - return ans; -} -inline void maximum_happiness(double *x, double *y, double *f, int n) -{ - double *dp=new double [3030]; - for(int i = 0; i < n; i++) - dp[i] = -1e9; - dp[0]=0; - for(int i=0; i>t; + while(t--) + { + + int n; + cin>>n; + pair,int> a[n]; + + for(int i=0;i>x>>y>>f; + + pair temp(x,y); + pair,int> big(temp,f); + a[i]=big; + + + } + + double dp[n]; + + dp[n-1]=a[n-1].second; + + for(int i=n-2;i>=0;i--) + { + int j=i+1; + double max=INT_MIN; + while(j<=n-1) + { + double t1=(double)a[i].first.first-a[j].first.first; + double t2=(double)a[i].first.second-a[j].first.second; + double t3=pow(t1,2)+pow(t2,2); + double t4=sqrt(t3); + + double temp=dp[j]-t4+a[i].second; + + if(temp>max) + { + max=temp; + } + j++; + } + dp[i]=max; + } + cout<>n; - double *x=new double [3030]; - double *y=new double [3030]; - double *f=new double [3030]; - for(int i=0; i>x[i]>>y[i]>>f[i]; - } - maximum_happiness(x, y, f, n); - delete[]x; - delete[]y; - delete[]f; -} \ No newline at end of file