CF1300B-Assigning to Classes 思维

排序后,中间相邻的两个分别作为两个班级的中位数必定最优。

因为两个中位数必定一个在前半段,一个在后半段,那么显然中间两个差值最小。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int a[210000],T,n;
 5 int main()
 6 {
 7     for (scanf("%d",&T);T;T--)
 8     {
 9         scanf("%d",&n);
10         for (int i = 1;i <= 2 * n;i++)
11             scanf("%d",&a[i]);
12         sort(a + 1,a + 2 * n + 1);
13         printf("%d\n",a[n + 1] - a[n]);
14     }
15     return 0;
16 }

猜你喜欢

转载自www.cnblogs.com/iat14/p/12289965.html