Google-Code-Jam/square_set.cpp

84 lines
2.2 KiB
C++
Raw Normal View History

2020-05-12 02:01:15 +00:00
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
2020-05-12 15:51:42 +00:00
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
2020-05-12 02:01:15 +00:00
cout << "Case #" << t + 1 << ": ";
2020-05-12 15:51:42 +00:00
int R, C;
cin >> R >> C;
vector<vector<int>> S(R); // we have to use vector of vector because R or C could be 1e5
set<pair<int, int>> check; // set of candidate competitors to check
vector<set<int>> rs(R), cs(C); // sets to quickly determine a competitor's neighbors
2020-05-12 02:01:15 +00:00
ll ans = 0, sum = 0;
for (int i = 0; i < R; ++i) {
2020-05-12 15:51:42 +00:00
S[i].resize(C);
for (int j = 0; j < C; ++j) {
2020-05-12 02:01:15 +00:00
cin >> S[i][j];
sum += S[i][j];
2020-05-12 15:51:42 +00:00
check.emplace(i, j);
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
}
2020-05-12 02:01:15 +00:00
2020-05-12 15:51:42 +00:00
// prepare sets
2020-05-12 02:01:15 +00:00
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) rs[i].insert(j);
}
for (int j = 0; j < C; ++j) {
for (int i = 0; i < R; ++i) cs[j].insert(i);
}
while (1) {
2020-05-12 15:51:42 +00:00
vector<pair<int, int>> elim; // vector to store eliminated competitors
for (auto& x : check) { // examine every candidate competitor
int i = x.first, j = x.second, cnt = 0, num = 0;
auto r = cs[j].find(i);
if (r != cs[j].end()) { // get neighbors
if (r != cs[j].begin()) cnt += S[*prev(r)][j], ++num;
if (r != --cs[j].end()) cnt += S[*next(r)][j], ++num;
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
auto c = rs[i].find(j);
if (c != rs[i].end()) { // get neighbors
if (c != rs[i].begin()) cnt += S[i][*prev(c)], ++num;
if (c != --rs[i].end()) cnt += S[i][*next(c)], ++num;
}
if (num * S[i][j] < cnt) elim.emplace_back(i, j);
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
check.clear(); // clear check for finding candidates for next round
2020-05-12 02:01:15 +00:00
ans += sum;
2020-05-12 15:51:42 +00:00
2020-05-12 02:01:15 +00:00
for (auto& x : elim) {
int i = x.first, j = x.second, cnt = 0, num = 0;
sum -= S[i][j];
S[i][j] = 0;
2020-05-12 15:51:42 +00:00
2020-05-12 02:01:15 +00:00
auto r = cs[j].find(i);
2020-05-12 15:51:42 +00:00
if (r != cs[j].end()) { // get neighbors
if (r != cs[j].begin()) check.emplace(*prev(r), j);
if (r != --cs[j].end()) check.emplace(*next(r), j);
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
2020-05-12 02:01:15 +00:00
auto c = rs[i].find(j);
2020-05-12 15:51:42 +00:00
if (c != rs[i].end()) { // get neighbors
if (c != rs[i].begin()) check.emplace(i, *prev(c));
if (c != --rs[i].end()) check.emplace(i, *next(c));
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
rs[i].erase(j), cs[j].erase(i); // remove competitor from sets
2020-05-12 02:01:15 +00:00
}
2020-05-12 15:51:42 +00:00
2020-05-12 02:01:15 +00:00
if (elim.empty()) break;
}
2020-05-12 15:51:42 +00:00
cout << ans << '\n';
}
2020-05-12 02:01:15 +00:00
}