# 12606 - Happy New Year ## 題解: Greedy 只要計算離自己最遠的距離,因為是來回,所以再乘於2就好了。 ## Code: ```c=1 #include <stdio.h> #include <stdlib.h> #define N 200000 + 5 int n, x[N]; int cmp(const void* a, const void* b){ return *(int*)a - *(int*)b; } int main(){ scanf("%d", &n); for(int i=0; i<=n; i++) scanf("%d", &x[i]); qsort(x + 1, n, sizeof(int), cmp); int ans = 0; if(x[0] >= x[1] && x[0] <= x[n]) ans = 2 * (x[0] - x[1]) + 2 * (x[n] - x[0]); else if(x[0] < x[1]) ans = 2 * (x[n] - x[0]); else if(x[0] > x[n]) ans = 2 * (x[0] - x[1]); printf("%d\n", ans); return 0; } ``` ###### tags: `NTHUOJ`