Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created March 26, 2017 14:17
Show Gist options
  • Select an option

  • Save henrybear327/a67f4e98a8c87eacbbaf41e7ac206e64 to your computer and use it in GitHub Desktop.

Select an option

Save henrybear327/a67f4e98a8c87eacbbaf41e7ac206e64 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 10000
double dist(int a, int b)
{
return sqrt((double)(a * a + b * b));
}
int cmp(const void *a, const void *b)
{
return *((int *)a) - *((int *)b);
}
int main()
{
int ncase;
char inp[N];
fgets(inp, N, stdin);
sscanf(inp, "%d", &ncase);
while(ncase--)
{
// read first row
fgets(inp, N, stdin);
int idx = 0;
int a[1111], b[1111];
char *num = strtok(inp, " ");
while(num != NULL)
{
a[idx++] = abs(atoi(num));
num = strtok(NULL, " ");
}
// read second row
fgets(inp, N, stdin);
idx = 0;
num = strtok(inp, " ");
while(num != NULL)
{
b[idx++] = abs(atoi(num));
num = strtok(NULL, " ");
}
qsort(a,idx,sizeof(int), cmp);
qsort(b,idx,sizeof(int), cmp);
// solve
double ans = 0.0;
for(int i = 0; i < idx; i++)
{
//printf("%f\n", dist(a[i], b[idx - i - 1]));
ans += dist(a[i], b[idx - i - 1]);
}
printf("%d\n", (int)floor(ans));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment