2010年9月17日 星期五

ACM 10041 - Vito's Family

#include <stdio.h>

int doorplate[502], index = 0;
char in [3000];

int abs(int n)
{
return n < 0 ? -n: n;
}

void input()
{
char ch;
int i, j = 0, k, tmp;
fgets(in, 2999, stdin);
for (i = 0; (ch = in[i]); i ++)
{
if (ch == ' ' || ch == '\n')
{
doorplate[index ++] = j;
for (k = index - 1; k >= 0; k --)
if (doorplate[k] < doorplate[k - 1])
tmp = doorplate[k], doorplate[k] = doorplate[k - 1]
, doorplate[k - 1] = tmp;
else break;
j = 0;
continue;
}
j = (j * 10) + ch - '0';
continue;
}
}

int main()
{
int caseNum, n, mid, i, total;
scanf("%d", &caseNum);
while (caseNum --)
{
total = 0, index = 0;
scanf("%d", &n);
getchar();
input();
mid = doorplate[n / 2];
for (i = 0; i < n; i ++)
{
total += abs(mid - doorplate[i]);
}
printf("%d\n", total);
}
return 0;
}


回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...