-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad4a7a5
commit 8f40695
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//http://www.spoj.com/problems/FASHION/ | ||
|
||
#include<iostream> | ||
#include<limits.h> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
void msort(int a[], int ini, int fin); | ||
int t, n, m[1000], w[1000], hb; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
hb = 0; | ||
cin>>n; | ||
for(int i=0; i<n; i++) | ||
cin>>m[i]; | ||
|
||
for(int j=0; j<n; j++) | ||
cin>>w[j]; | ||
|
||
msort(m, 0, n-1); | ||
msort(w, 0, n-1); | ||
|
||
for(int k=0; k<n; k++) | ||
{ | ||
hb += m[k]*w[k]; | ||
} | ||
cout<<hb<<endl; | ||
} | ||
return 0; | ||
} | ||
|
||
void msort(int a[], int ini, int fin) | ||
{ | ||
void merge(int a[], int ini, int mid, int fin); | ||
int mid = (ini + fin)/2; | ||
if (ini<fin) | ||
{ | ||
msort(a, ini, mid); | ||
msort(a, mid+1, fin); | ||
merge(a, ini, mid, fin); | ||
} | ||
} | ||
|
||
void merge(int a[], int ini=0, int mid=0, int fin=0) | ||
{ | ||
int i=0, j=0, k=0; | ||
int l1 = mid - ini + 2; | ||
int l2 = fin - mid +1; | ||
int l[l1], r[l2]; | ||
|
||
l[l1-1] = r[l2 - 1] = INT_MAX; | ||
|
||
for(i=0;i<l1-1;i++) | ||
{ | ||
l[i] = a[ini+i]; | ||
} | ||
|
||
for(i=0; i<l2-1;i++) | ||
{ | ||
r[i] = a[mid+i+1]; | ||
} | ||
|
||
for(i=0, j=0, k=ini; k<=fin; k++) | ||
{ | ||
if(l[i]<r[j]) | ||
{ | ||
a[k] = l[i]; | ||
i++; | ||
} | ||
else | ||
{ | ||
a[k] = r[j]; | ||
j++; | ||
} | ||
} | ||
} |