結果

問題 No.1479 Matrix Eraser
ユーザー ayoiyouyoeyoayoiyouyoeyo
提出日時 2021-04-16 21:35:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 592 ms / 3,000 ms
コード長 2,751 bytes
コンパイル時間 5,022 ms
コンパイル使用メモリ 190,200 KB
実行使用メモリ 30,700 KB
最終ジャッジ日時 2023-09-15 23:46:55
合計ジャッジ時間 15,605 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 55 ms
6,492 KB
testcase_08 AC 102 ms
8,692 KB
testcase_09 AC 236 ms
14,200 KB
testcase_10 AC 479 ms
22,576 KB
testcase_11 AC 278 ms
16,044 KB
testcase_12 AC 71 ms
7,336 KB
testcase_13 AC 101 ms
8,784 KB
testcase_14 AC 75 ms
7,536 KB
testcase_15 AC 15 ms
4,380 KB
testcase_16 AC 85 ms
8,008 KB
testcase_17 AC 592 ms
25,804 KB
testcase_18 AC 586 ms
25,836 KB
testcase_19 AC 585 ms
25,884 KB
testcase_20 AC 583 ms
25,896 KB
testcase_21 AC 570 ms
25,812 KB
testcase_22 AC 584 ms
26,084 KB
testcase_23 AC 587 ms
25,936 KB
testcase_24 AC 586 ms
25,940 KB
testcase_25 AC 589 ms
25,848 KB
testcase_26 AC 586 ms
26,012 KB
testcase_27 AC 168 ms
9,204 KB
testcase_28 AC 166 ms
9,072 KB
testcase_29 AC 169 ms
8,804 KB
testcase_30 AC 168 ms
8,856 KB
testcase_31 AC 170 ms
9,216 KB
testcase_32 AC 99 ms
19,996 KB
testcase_33 AC 101 ms
20,080 KB
testcase_34 AC 100 ms
19,824 KB
testcase_35 AC 99 ms
19,564 KB
testcase_36 AC 101 ms
20,096 KB
testcase_37 AC 44 ms
10,972 KB
testcase_38 AC 194 ms
7,340 KB
testcase_39 AC 418 ms
30,700 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<bitset>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
#include<cassert>
#include<random>
using namespace std;
#include<atcoder/all>
using namespace atcoder;


#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e17;
const ll MAX = 4000001;
const long double eps = 1E-14;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll id;
	ll fr;
	ll to;
	ll d;
};

using mint = modint;

typedef vector<ll> vll;
typedef vector <vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

typedef vector<mint> vmint;
typedef vector<vector<mint>> vvmint;
typedef vector<vector<vector<mint>>> vvvmint;

vmint f, finv, inv;

void cominit(ll N) {
	ll MOD = modint::mod();//デフォルトが998244353に注意

	f.assign(N + 1, 1);
	finv.assign(N + 1, 1);
	inv.assign(N + 1, 1);
	inv[1] = 1;

	repn(i, N) {
		f[i] = f[i - 1] * i;
		if (i > 1)inv[i] = -inv[MOD % i] * (MOD / i);
		finv[i] = finv[i - 1] * inv[i];
	}
}

mint com(ll a, ll b) {
	if (a < 0 || b < 0 || a < b) { return 0; }
	return f[a] * finv[b] * finv[a - b];
}


/////////////////////////////////////


int main() {
	ll H, W;
	cin >> H >> W;

	map<ll, vector<P>> mp;

	rep(i, H)rep(j, W) {
		ll a;
		cin >> a;
		mp[a].push_back({ i,j });
	}

	ll ans = 0;

	for (auto p : mp) {
		if (p.first == 0) { continue; }

		vector<P> v = p.second;
		vll lisx, lisy;
		rep(i, v.size()) {
			lisx.push_back(v[i].first);
			lisy.push_back(v[i].second);
		}
		sort(lisx.begin(), lisx.end());
		sort(lisy.begin(), lisy.end());

		vll zx, zy;
		rep(i, lisx.size()) {
			if (i == 0 || lisx[i] != lisx[i - 1]) { zx.push_back(lisx[i]); }
			if (i == 0 || lisy[i] != lisy[i - 1]) { zy.push_back(lisy[i]); }
		}

		ll X = zx.size();
		ll Y = zy.size();

		mf_graph<ll> g(X + Y + 2);
		rep(i, X) { g.add_edge(X + Y, i,1); }
		rep(i, Y) { g.add_edge(X + i, X + Y + 1, 1); }

		rep(i, v.size()) {
			//cout << v[i].first << " " << v[i].second << endl;

			auto itr = lower_bound(zx.begin(), zx.end(), v[i].first);
			ll x = distance(zx.begin(), itr);

			auto itr2 = lower_bound(zy.begin(), zy.end(), v[i].second);
			ll y = distance(zy.begin(), itr2);

			g.add_edge(x, X + y, 1);
		}

		ll now = g.flow(X + Y, X + Y + 1);
		//cout << now << endl;

		ans += now;
	}

	cout << ans << endl;
}
0