Codeforces Round #684(div.2B)


题目

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

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

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

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

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

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

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

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

思考过程

通过思考,可以很轻易地想到:先每一行地判断每一个数周围的数是否相等,若相等则对某一个数做出某些改变,而后再每一列地进行相同地判断,但稍微仔细一想,这样的方式并不能保证每个数至多+1,因此我们需要换一种思路。
基于每个数最多+1的条件,我们可以将奇偶判断代入到这个题里面,只要保证奇数的周围全是偶数,偶数的周围全是奇数便可。

代码如下

代码如下(示例):

扫描二维码关注公众号,回复: 12435461 查看本文章
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
 
int test=1;
int n,k;
int a[N];
LL res=0;
int main()
{
    
    
	scanf("%d",&test);
	while(test--)
	{
    
    
		res=0;
		cin >> n >> k;
		for(int i=1;i<=n*k;i++)
			scanf("%d",&a[i]);
		for(int i=k*n-n/2;i>(n-1)/2*k;i-=n/2+1)
			res += a[i];
		cout << res << '\n';
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/p15008340649/article/details/109964425