結果

問題 No.1479 Matrix Eraser
ユーザー hir355hir355
提出日時 2021-04-16 22:24:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,941 bytes
コンパイル時間 2,764 ms
コンパイル使用メモリ 221,536 KB
実行使用メモリ 23,496 KB
最終ジャッジ日時 2023-09-16 01:15:26
合計ジャッジ時間 9,449 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
14,868 KB
testcase_01 AC 6 ms
14,832 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 59 ms
16,432 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 60 ms
16,452 KB
testcase_14 WA -
testcase_15 AC 15 ms
15,028 KB
testcase_16 AC 52 ms
16,080 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 80 ms
23,024 KB
testcase_33 AC 79 ms
23,276 KB
testcase_34 AC 80 ms
23,332 KB
testcase_35 AC 80 ms
23,316 KB
testcase_36 AC 80 ms
23,436 KB
testcase_37 AC 41 ms
15,808 KB
testcase_38 AC 146 ms
18,004 KB
testcase_39 AC 270 ms
23,496 KB
testcase_40 AC 7 ms
14,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/maxflow>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
const long long INF_LL = 2000000000000000000LL;
const int INF = 2000000000;
const long long MOD = 1000000007;
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for(int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll modpow(ll a, ll b, ll m) {
    if(b == 0)
        return 1;
    ll t = modpow(a, b / 2, m);
    if(b & 1) {
        return (t * t % m) * a % m;
    } else {
        return t * t % m;
    }
}

struct edge {
    int to;
    ll cost;
    edge(int t, ll c) { to = t, cost = c; }
};

typedef vector<vector<edge>> graph;

// using mint = modint998244353;

int main(){
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    rep(i, h) rep(j, w) cin >> a[i][j];
    vector<vector<pair<int, int>>> t(500000);
    rep(i, h){
        rep(j, w){
            if(a[i][j] > 0) {
                t[a[i][j] - 1].push_back({i, j});
            }
        }
    }
    int ans = 0;
    for(int i = 499999; i >= 0; i--){
        if(t[i].size() > 0){
            int tmp1;
            sort(all(t[i]));
            int tmp = t[i][0].first;
            t[i][0].first = 0;
            rep(j, t[i].size() - 1){
                if(tmp < t[i][j + 1].first){
                    t[i][j + 1].first = t[i][j].first + 1;
                    tmp++;
                }else{
                    t[i][j + 1].first = t[i][j].first;
                }
            }
            tmp1 = t[i][t[i].size()-1].first + 1;
            int tmp2;
            sort(all(t[i]), [](auto x, auto y){return x.second < y.second;});
            tmp = t[i][0].second;
            t[i][0].second = 0;
            rep(j, t[i].size() - 1){
                if(tmp < t[i][j + 1].second){
                    t[i][j + 1].second = t[i][j].second + 1;
                    tmp++;
                }else{
                    t[i][j + 1].second = t[i][j].second;
                }
            }
            tmp2 = t[i][t[i].size()-1].second + 1;
            mf_graph<int> g(tmp1 + tmp2 + 2);
            rep(j, tmp1) g.add_edge(tmp1 + tmp2, j, 1);
            rep(j, tmp2) g.add_edge(j + tmp1, tmp1 + tmp2 + 1, 1);
            rep(j, t[i].size()){
                g.add_edge(t[i][j].first, t[i][j].second + tmp1, 1);
            }
            ans += g.flow(tmp1 + tmp2, tmp1 + tmp2 + 1);
        }
    }
    cout << ans << endl;
}
0