Skip to content

Instantly share code, notes, and snippets.

@ujjwal123123
Created February 9, 2020 11:53
Show Gist options
  • Select an option

  • Save ujjwal123123/1a1190b71885ac4844d1447c8b3377f6 to your computer and use it in GitHub Desktop.

Select an option

Save ujjwal123123/1a1190b71885ac4844d1447c8b3377f6 to your computer and use it in GitHub Desktop.
School of Geometry Problem
// Code: Problem Code: SNUG_FIT
// Example Input
// 2
// 4
// 8 8 10 12
// 15 20 3 5
// 3
// 20 20 20
// 10 10 10
// Example Output
// 30
// 30
#include <stdio.h>
#include <stdlib.h>
int min(int a, int b)
{
if (b < a)
return b;
else
return a;
}
int swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
return 0;
}
// selection sort
// int sort(int* arr, int length)
// {
// for (int i = 0; i < length; ++i)
// for (int j = i + 1; j < length; ++j)
// if (arr[j] < arr[i])
// swap(&arr[i], &arr[j]);
// return 0;
// }
int sort(long long int* arr, int length)
{
for (int i = 1; i < length; i++) {
int key = arr[i];
// insert arr[i] into sorted sequence
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return 0;
}
int solve()
{
int rect;
scanf(" %d", &rect);
long long int* a = (long long int*)malloc(sizeof(long long int) * rect);
long long int* b = (long long int*)malloc(sizeof(long long int) * rect);
for (int i = 0; i < rect; i++)
scanf(" %lld", &a[i]);
sort(a, rect);
for (int i = 0; i < rect; i++)
scanf(" %lld", &b[i]);
sort(b, rect);
int sum = 0;
for (int i = 0; i < rect; i++)
sum += min(a[i], b[i]);
printf("%d\n", sum);
return 0;
}
int main()
{
int times;
scanf(" %d", &times);
while (times--)
solve();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment