Submission #36451488
Source Code Expand
// #define NDEBUG
#include <bits/stdc++.h>
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using Int = long long;
using Uint = unsigned long long;
using Real = long double;
template<typename T, typename U>
inline bool chmax(T &a, U b) { return a < b and ((a = b), true); }
template<typename T, typename U>
inline bool chmin(T &a, U b) { return a > b and ((a = b), true); }
template<typename T>
constexpr T kBig = std::numeric_limits<T>::max() / 2;
#if __cplusplus < 202002L
template<typename T>
inline int ssize(const T &a) { return (int) a.size(); }
#endif
template<size_t BufSize>
class InputReader {
public:
void skip() {
// Initialize on the first use.
[[maybe_unused]] static const bool init = [this]() {
const size_t len = fread(buf, 1, sizeof(buf) - 1, stdin);
buf[len] = '\0';
p = buf;
bufend = buf + len;
return true;
}();
while (isspace(*p)) ++p;
}
template<typename T>
operator T() {
T x;
skip();
assert(not is_eof()); // Couldn't read reached the end of input.
read_one(x);
return x;
}
struct Sized {
InputReader<BufSize> &reader;
int n;
template<typename T>
operator T() const {
T xs(n);
for (auto &x: xs) {
reader.skip();
assert(not reader.is_eof());
reader.read_one(x);
}
return xs;
}
};
Sized operator()(int n) { return {*this, n}; }
bool is_eof() { return p >= bufend; }
private:
static inline char buf[BufSize];
char *p, *bufend;
template<class T>
std::enable_if_t<std::is_integral_v<T>> read_one(T &x) {
[[maybe_unused]] int sign = 1;
if constexpr (std::is_signed_v<T>) {
sign = (*p == '-') ? (++p, -1) : 1;
}
x = 0;
while (isdigit(*p)) x = x * 10 + (*p++ & 0x0F);
if constexpr (std::is_signed_v<T>) {
x *= sign;
}
}
void read_one(std::string &s) {
char *p0 = p;
while (not isspace(*p)) p++;
s.assign(p0, p);
}
void read_one(std::string_view &s) {
const char *p0 = p;
while (not isspace(*p)) p++;
s = std::string_view(p0, p - p0);
}
};
InputReader<(1 << 26)> in;
template<typename Container>
std::ostream &out_seq(const Container &seq, const char *sep = " ",
const char *ends = "\n", std::ostream &os = std::cout) {
const auto itl = std::begin(seq), itr = std::end(seq);
for (auto it = itl; it != itr; ++it) {
if (it != itl) os << sep;
os << *it;
}
return os << ends;
}
template<typename T>
std::ostream &out_one(const T &x, char endc) {
if constexpr (std::is_same<T, bool>::value) {
return std::cout << (x ? "Yes" : "No") << endc;
} else {
return std::cout << x << endc;
}
}
template<typename T>
std::ostream &out(const T &x) {
return out_one(x, '\n');
}
template<typename T, typename... Ts>
std::ostream &out(const T &head, Ts... tail) {
return out_one(head, ' '), out(tail...);
}
void init_() {
// std::ios::sync_with_stdio(false);
// std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(18);
}
[[noreturn]] void exit_() {
#ifdef MY_DEBUG
in.skip();
assert(in.is_eof()); // Some input is left.
#endif
fflush(stdout), fflush(stderr);
std::cout.flush(), std::cerr.flush();
std::_Exit(0);
}
#ifdef MY_DEBUG
#include "debug_dump.hpp"
#include "backward.hpp"
backward::SignalHandling kSignalHandling;
#else
#define DUMP(...)
#define test_case(...)
#define cerr if(false)cerr
#endif
using namespace std;
auto solve() -> bool {
int H = in, W = in;
auto A = vector(H, vector(W, 0LL));
REP(i, H) REP(j, W) A[i][j] = Int(in);
for (int i = H - 1; i >= 0; --i) {
bool all_zero = [&]() {
REP(j, W) if (A[i][j] != 0) return false;
return true;
}();
if (all_zero) {
swap(A[i], A.back());
A.pop_back();
--H;
}
}
if (A.empty()) return true;
for (int j = W - 1; j >= 0; --j) {
bool all_zero = [&]() {
REP(i, H) if (A[i][j] != 0) return false;
return true;
}();
if (all_zero) {
REP(i, H) {
swap(A[i][j], A[i].back());
A[i].pop_back();
}
--W;
}
}
if (W == 0) return true;
DUMP(H, W);
REP(i, H) {
DUMP(A[i]);
}
vector<Int> rsum(H), rc(H);
REP(i, H) {
REP(j, W) {
rsum[i] += A[i][j];
if (A[i][j] > 0) ++rc[i];
}
}
{
vector<int> idx(A.size());
std::iota(idx.begin(), idx.end(), 0);
sort(ALL(idx), [&](int i, int j) {
return rsum[i] * rc[j] < rsum[j] * rc[i];
});
DUMP(rsum);
DUMP(rc);
DUMP(idx);
Int maxval = -1;
for (int i: idx) {
Int mi = kBig<Int>, ma = -1;
REP(j, W) {
if (A[i][j] == 0) continue;
chmin(mi, A[i][j]);
chmax(ma, A[i][j]);
}
if (mi < maxval) {
return false;
}
chmax(maxval, ma);
}
}
list<map<Int, set<int>>> seq;
REP(i, H) {
map<Int, set<int>> si;
REP(j, W) {
Int x = A[i][j];
if (x == 0) continue;
si[x].insert(j);
}
if (si.empty()) continue;
seq.push_back(si);
}
vector<int> bigger(W);
for (auto it = seq.begin(); it != seq.end(); ++it) {
assert(not it->empty());
for (auto jt = next(it->begin()); jt != it->end(); ++jt) {
for (int k: jt->second) ++bigger[k];
}
}
queue<int> minq;
REP(i, W) {
if (bigger[i] == 0) {
minq.push(i);
}
}
set<int> oks;
while (not minq.empty()) {
int v = minq.front();
minq.pop();
oks.insert(v);
for (auto it = seq.begin(); it != seq.end();) {
bool erased = false;
it->begin()->second.erase(v);
if (it->begin()->second.empty()) {
it->erase(it->begin());
if (it->empty()) {
it = seq.erase(it);
erased = true;
} else {
for (int k: it->begin()->second) {
--bigger[k];
if (bigger[k] == 0) {
minq.push(k);
}
}
}
}
if (not erased) ++it;
}
}
DUMP(oks);
return ssize(oks) == W;
}
int main() {
init_();
const int T = 1;//in;
REP(t, T) {
test_case(t, T);
out(solve());
}
exit_();
}
Submission Info
| Submission Time |
|
| Task |
F - Sorting a Matrix |
| User |
keijak |
| Language |
C++ (GCC 9.2.1) |
| Score |
500 |
| Code Size |
6559 Byte |
| Status |
AC |
| Exec Time |
1963 ms |
| Memory |
228712 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
500 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, 064.txt, 065.txt, 066.txt, 067.txt, 068.txt, example0.txt, example1.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
430 ms |
83092 KiB |
| 001.txt |
AC |
25 ms |
16748 KiB |
| 002.txt |
AC |
1963 ms |
228712 KiB |
| 003.txt |
AC |
888 ms |
107296 KiB |
| 004.txt |
AC |
40 ms |
19148 KiB |
| 005.txt |
AC |
524 ms |
224516 KiB |
| 006.txt |
AC |
468 ms |
224520 KiB |
| 007.txt |
AC |
141 ms |
46800 KiB |
| 008.txt |
AC |
378 ms |
134420 KiB |
| 009.txt |
AC |
118 ms |
42224 KiB |
| 010.txt |
AC |
19 ms |
13080 KiB |
| 011.txt |
AC |
413 ms |
60112 KiB |
| 012.txt |
AC |
720 ms |
158596 KiB |
| 013.txt |
AC |
22 ms |
12160 KiB |
| 014.txt |
AC |
29 ms |
15556 KiB |
| 015.txt |
AC |
23 ms |
11864 KiB |
| 016.txt |
AC |
31 ms |
17700 KiB |
| 017.txt |
AC |
31 ms |
17620 KiB |
| 018.txt |
AC |
27 ms |
17624 KiB |
| 019.txt |
AC |
578 ms |
123892 KiB |
| 020.txt |
AC |
475 ms |
106108 KiB |
| 021.txt |
AC |
458 ms |
86972 KiB |
| 022.txt |
AC |
269 ms |
64552 KiB |
| 023.txt |
AC |
165 ms |
40628 KiB |
| 024.txt |
AC |
470 ms |
74572 KiB |
| 025.txt |
AC |
328 ms |
63108 KiB |
| 026.txt |
AC |
279 ms |
52784 KiB |
| 027.txt |
AC |
228 ms |
43052 KiB |
| 028.txt |
AC |
140 ms |
31548 KiB |
| 029.txt |
AC |
366 ms |
63868 KiB |
| 030.txt |
AC |
316 ms |
53804 KiB |
| 031.txt |
AC |
257 ms |
43556 KiB |
| 032.txt |
AC |
207 ms |
33988 KiB |
| 033.txt |
AC |
143 ms |
24748 KiB |
| 034.txt |
AC |
508 ms |
61852 KiB |
| 035.txt |
AC |
516 ms |
52612 KiB |
| 036.txt |
AC |
363 ms |
42480 KiB |
| 037.txt |
AC |
259 ms |
33224 KiB |
| 038.txt |
AC |
142 ms |
22984 KiB |
| 039.txt |
AC |
597 ms |
61672 KiB |
| 040.txt |
AC |
435 ms |
51484 KiB |
| 041.txt |
AC |
346 ms |
41900 KiB |
| 042.txt |
AC |
236 ms |
33032 KiB |
| 043.txt |
AC |
162 ms |
22824 KiB |
| 044.txt |
AC |
440 ms |
123956 KiB |
| 045.txt |
AC |
32 ms |
16840 KiB |
| 046.txt |
AC |
420 ms |
86512 KiB |
| 047.txt |
AC |
36 ms |
14856 KiB |
| 048.txt |
AC |
129 ms |
40820 KiB |
| 049.txt |
AC |
30 ms |
16876 KiB |
| 050.txt |
AC |
225 ms |
63384 KiB |
| 051.txt |
AC |
43 ms |
17500 KiB |
| 052.txt |
AC |
177 ms |
42404 KiB |
| 053.txt |
AC |
31 ms |
13536 KiB |
| 054.txt |
AC |
411 ms |
63864 KiB |
| 055.txt |
AC |
29 ms |
15328 KiB |
| 056.txt |
AC |
246 ms |
44140 KiB |
| 057.txt |
AC |
34 ms |
13852 KiB |
| 058.txt |
AC |
110 ms |
24188 KiB |
| 059.txt |
AC |
27 ms |
14776 KiB |
| 060.txt |
AC |
32 ms |
15484 KiB |
| 061.txt |
AC |
32 ms |
13976 KiB |
| 062.txt |
AC |
134 ms |
32776 KiB |
| 063.txt |
AC |
24 ms |
12936 KiB |
| 064.txt |
AC |
20 ms |
14004 KiB |
| 065.txt |
AC |
23 ms |
13736 KiB |
| 066.txt |
AC |
33 ms |
13600 KiB |
| 067.txt |
AC |
30 ms |
13436 KiB |
| 068.txt |
AC |
29 ms |
13104 KiB |
| example0.txt |
AC |
2 ms |
3308 KiB |
| example1.txt |
AC |
2 ms |
3268 KiB |