結果

問題 No.1479 Matrix Eraser
ユーザー kakira9618kakira9618
提出日時 2021-04-16 20:57:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,479 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 176,388 KB
実行使用メモリ 23,584 KB
最終ジャッジ日時 2023-09-15 22:23:50
合計ジャッジ時間 41,759 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
14,924 KB
testcase_01 AC 60 ms
14,808 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 592 ms
15,836 KB
testcase_08 AC 655 ms
16,516 KB
testcase_09 AC 879 ms
18,304 KB
testcase_10 AC 1,287 ms
21,280 KB
testcase_11 AC 953 ms
18,916 KB
testcase_12 AC 608 ms
16,124 KB
testcase_13 AC 562 ms
16,500 KB
testcase_14 AC 582 ms
16,148 KB
testcase_15 AC 286 ms
15,156 KB
testcase_16 AC 534 ms
16,172 KB
testcase_17 AC 1,474 ms
21,988 KB
testcase_18 AC 1,463 ms
21,960 KB
testcase_19 AC 1,474 ms
22,032 KB
testcase_20 AC 1,461 ms
22,012 KB
testcase_21 AC 1,465 ms
21,972 KB
testcase_22 AC 1,467 ms
22,052 KB
testcase_23 AC 1,458 ms
21,960 KB
testcase_24 AC 1,463 ms
21,992 KB
testcase_25 AC 1,452 ms
21,920 KB
testcase_26 AC 1,469 ms
21,920 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1,188 ms
20,932 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1,162 ms
20,556 KB
testcase_37 AC 1,146 ms
17,904 KB
testcase_38 AC 1,419 ms
17,660 KB
testcase_39 AC 1,419 ms
23,584 KB
testcase_40 AC 49 ms
14,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

void solve() {
    int H, W;
    cin >> H >> W;
    constexpr int size = 500001;
    vector<vector<int>> A(H, vector<int>(W));
    vector<vector<pii>> X(size);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
            X[A[i][j]].push_back({i, j});
        }
    }

    int ans = 0;
    for (int a = 1; a < size; a++) {
        vector<int> cntH(H), cntW(W);
        vector<vector<pii>> xH(H), xW(W);
        for (int k = 0; k < X[a].size(); k++) {
            int i = X[a][k].first, j = X[a][k].second;
            cntH[i]++; cntW[j]++;
            xH[i].push_back(X[a][k]);
            xW[j].push_back(X[a][k]);
        }
        
        int rest = X[a].size();
        int ret = 0;
        while(rest > 0) {
            int ma = -1, ma_i = -1, ma_j = -1;
            for (int i = 0; i < H; i++) {
                if (ma < cntH[i]) {
                    ma = cntH[i];
                    ma_i = i;
                }
            }
            for (int j = 0; j < W; j++) {
                if (ma < cntW[j]) {
                    ma = cntW[j];
                    ma_j = j;
                }
            }
            if (ma_j != -1) {
                for (int k = 0; k < xW[ma_j].size(); k++) {
                    int i = xW[ma_j][k].first;
                    cntH[i]--;
                    cntW[ma_j]--;
                    rest--;
                }
            } else {
                for (int k = 0; k < xH[ma_i].size(); k++) {
                    int j = xH[ma_i][k].second;
                    cntH[ma_i]--;
                    cntW[j]--;
                    rest--;
                }
            }
            ret++;
        }
        ans += ret;
    }

    cout << ans << endl;
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();
}
0