Codeforces1440B Sum of Medians(贪心,数学, 思维)

B. Sum of Medians
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

median of an array of integers of length nn is the number standing on the n2⌈n2⌉ (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting with 11. For example, a median of the array [2,6,4,1,3,5][2,6,4,1,3,5] is equal to 33. There exist some other definitions of the median, but in this problem, we will use the described one.

Given two integers nn and kk and non-decreasing array of nknk integers. Divide all numbers into kk arrays of size nn, such that each number belongs to exactly one array.

You want the sum of medians of all kk arrays to be the maximum possible. Find this maximum possible sum.

Input

The first line contains a single integer tt (1t1001≤t≤100) — the number of test cases. The next 2t2t lines contain descriptions of test cases.

The first line of the description of each test case contains two integers nn, kk (1n,k10001≤n,k≤1000).

The second line of the description of each test case contains nknk integers a1,a2,,anka1,a2,…,ank (0ai1090≤ai≤109) — given array. It is guaranteed that the array is non-decreasing: a1a2anka1≤a2≤…≤ank.

It is guaranteed that the sum of nknk for all test cases does not exceed 21052⋅105.

Output

For each test case print a single integer — the maximum possible sum of medians of all kk arrays.

Example
input
Copy
6
2 4
0 24 34 58 62 64 69 78
2 2
27 61 81 91
4 3
2 4 16 18 21 27 36 53 82 91 92 95
3 4
3 11 12 22 33 35 38 67 69 71 94 99
2 1
11 41
3 3
1 1 1 1 1 1 1 1 1
output
Copy
165
108
145
234
11
3
Note

The examples of possible divisions into arrays for all test cases of the first test:

Test case 11: [0,24],[34,58],[62,64],[69,78][0,24],[34,58],[62,64],[69,78]. The medians are 0,34,62,690,34,62,69. Their sum is 165165.

Test case 22: [27,61],[81,91][27,61],[81,91]. The medians are 27,8127,81. Their sum is 108108.

Test case 33: [2,91,92,95],[4,36,53,82],[16,18,21,27][2,91,92,95],[4,36,53,82],[16,18,21,27]. The medians are 91,36,1891,36,18. Their sum is 145145.

Test case 44: [3,33,35],[11,94,99],[12,38,67],[22,69,71][3,33,35],[11,94,99],[12,38,67],[22,69,71]. The medians are 33,94,38,6933,94,38,69. Their sum is 234234.

Test case 55: [11,41][11,41]. The median is 1111. The sum of the only median is 1111.

Test case 66: [1,1,1],[1,1,1],[1,1,1][1,1,1],[1,1,1],[1,1,1]. The medians are 1,1,11,1,1. Their sum is 33.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define scanf scanf_s
typedef long long ll;

const int N = 1000001;
int T;
ll a[N];
bool cmp(int a, int b) {
    return a < b;
}

int main() {
    scanf("%d", &T);
    while (T--) {
        int n, k;
        scanf("%d%d", &n, &k);
        for (int i = 1; i <= n * k; i++) {
            scanf("%lld", &a[i]);
        }
        int qian = k * (ceil(n * 1.0 / 2) - 1);
        ll ans = 0;
        for (int i = qian + 1; i <= n * k; i += (n-(ceil(n*1.0/2)-1))) {
            ans += a[i];
        }
        printf("%lld
", ans);

    }

    return 0;
}
原文地址:https://www.cnblogs.com/sineagle/p/14501960.html