본문 바로가기

알고리즘/코드포스

[코드포스] Sum of Medians

문제링크 : codeforces.com/contest/1440/problem/B

 

Problem - B - Codeforces

 

codeforces.com

영어 지문의 경우 테스트케이스에 대한 해석으로만 풀려는 경향이 있는 것 같다.

또, 조건을 충분히 파악하지 않고 풀다가 long long의 결과값이 나올 수 있는 문제인데

int 범위만 생각하다가 몇번 틀리고 오랫동안 매달렸다.

아직 코드포스는, 익숙한 c++로 풀었다.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int main() {
	int tc;
	cin >> tc;
	for (int i = 0; i < tc; i++) {
		vector<long long> a;
		int x, y;
		cin >> x >> y;
		for (int i = 0; i < x * y; i++) {
			long long temp;
			cin >> temp;
			a.push_back(temp);
	
		}
		long long ans = 0;
		long long start = (x - 1) / 2;
		long long gan = x - start;
		int end = a.size() - gan;
		for (long long j = end; j >=y*start; j -= gan) {
			ans += a[j];
		}
		cout << ans << '\n';
	}
}