QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#522156 | #8485. Magic Square | STnofarjo# | RE | 0ms | 3576kb | C++14 | 1.3kb | 2024-08-16 19:15:23 | 2024-08-16 19:15:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<vector<long long>> a(n, vector<long long>(n));
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> a[i][j];
}
}
long long sqr = n * n;
long long real_sum = sqr * (sqr+1) / 2 / n;
vector<int> ai, aj;
for (int i=0; i<n; i++) {
long long sum = 0;
for (int j=0; j<n; j++) sum += a[i][j];
if (sum != real_sum) ai.push_back(i);
}
for (int i=0; i<n; i++) {
long long sum = 0;
for (int j=0; j<n; j++) sum += a[j][i];
if (sum != real_sum) aj.push_back(i);
}
for (int i=0; i<2; i++) {
swap(a[ai[0]][aj[0]], a[ai[1]][aj[1]]);
bool ok = true;
for (int r=0; r<n && ok; r++) {
long long sum = 0;
for (int c=0; c<n && ok; c++) sum += a[r][c];
if (sum != real_sum) ok = false;
}
for (int c=0; c<n && ok; c++) {
long long sum = 0;
for (int r=0; r<n && ok; r++) sum += a[r][c];
if (sum != real_sum) ok = false;
}
if (ok) {
cout << ai[0] + 1 << ' ' << aj[0] + 1 << '\n' << ai[1] + 1 << ' ' << aj[1] + 1 << '\n';
return;
}
swap(a[ai[0]][aj[0]], a[ai[1]][aj[1]]);
swap(aj[0], aj[1]);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int tcs = 1;
// cin >> tcs;
while (tcs--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
3 6 9 2 3 5 7 8 1 4
output:
1 1 3 3
result:
ok OK
Test #2:
score: -100
Runtime Error
input:
4 16 3 2 13 5 10 11 8 9 6 7 12 1 15 14 4