取数游戏 - 最大得分算法 C++ 实现
#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
int row, col, val;
};
bool cmp(Node a, Node b) {
return a.val > b.val;
}
int main() {
int n;
cin >> n;
Node matrix[101][101];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> matrix[i][j].val;
matrix[i][j].row = i;
matrix[i][j].col = j;
}
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
Node tmp[101];
for (int j = 1; j <= n; ++j) {
tmp[j] = matrix[j][i];
}
sort(tmp + 1, tmp + n + 1, cmp);
long long sum = 0;
for (int j = 1; j <= n; ++j) {
matrix[tmp[j].row][tmp[j].col].val = -1;
sum += tmp[j].val;
}
ans += sum * i;
}
cout << ans << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/k43S 著作权归作者所有。请勿转载和采集!