/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 516.0 KiB
#2 Wrong Answer 3ms 540.0 KiB
#3 Wrong Answer 31ms 704.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
	int n, m; cin >> n >> m;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int x; cin >> x;
			v.push_back(x);
		}
	}
	sort(v.begin(), v.end());
	deque <int> q(v.begin(), v.end());
	int ans[n][m];
	queue <array<int, 2>> pq;
	pq.push({0, 0});
	int used[n][m] {};
	used[0][0] = 1;

	while (!pq.empty()) {
		auto [x, y] = pq.front();
		pq.pop();
		if ((x + y) % 2 == 0) {
			ans[x][y] = q.front();
			q.pop_front();
		}
		else {
			ans[x][y] = q.back();
			q.pop_back();
		}
		if (y + 1 < m and !used[x][y + 1]) pq.push({x, y + 1}), used[x][y + 1] = 1;
		if (x + 1 < n and !used[x + 1][y]) {
			pq.push({x + 1, y});
			used[x + 1][y] = 1;
		}
	}

	int a = 1e9;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (i + 1 < n) a = min(a, ans[i + 1][j] + ans[i][j]);
			if (j + 1 < m) a = min(a, ans[i][j + 1] + ans[i][j]);
		}
	}
	cout << a << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int tt = 1;
	cin >> tt;
	while (tt--) {
		solve();
	}
}

Information

Submit By
Type
Submission
Problem
P1065 Matrix Sum
Contest
Brain Booster #4
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-14 17:53:04
Judged At
2024-11-11 03:22:38
Judged By
Score
5
Total Time
31ms
Peak Memory
704.0 KiB