結果

問題 No.2251 Marking Grid
ユーザー milanis48663220milanis48663220
提出日時 2023-03-17 23:00:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 586 ms / 3,000 ms
コード長 4,999 bytes
コンパイル時間 1,846 ms
コンパイル使用メモリ 144,300 KB
実行使用メモリ 44,860 KB
最終ジャッジ日時 2023-10-18 16:01:24
合計ジャッジ時間 6,888 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 249 ms
24,564 KB
testcase_23 AC 291 ms
26,384 KB
testcase_24 AC 14 ms
4,468 KB
testcase_25 AC 288 ms
26,360 KB
testcase_26 AC 72 ms
9,152 KB
testcase_27 AC 164 ms
16,772 KB
testcase_28 AC 11 ms
4,408 KB
testcase_29 AC 175 ms
17,856 KB
testcase_30 AC 60 ms
8,396 KB
testcase_31 AC 42 ms
6,896 KB
testcase_32 AC 26 ms
6,496 KB
testcase_33 AC 586 ms
44,860 KB
testcase_34 AC 37 ms
7,760 KB
testcase_35 AC 232 ms
23,860 KB
testcase_36 AC 333 ms
29,332 KB
testcase_37 AC 479 ms
38,704 KB
testcase_38 AC 16 ms
5,100 KB
testcase_39 AC 9 ms
4,456 KB
testcase_40 AC 201 ms
21,980 KB
testcase_41 AC 83 ms
12,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

vector<vector<int>> transpose(vector<vector<int>> a){
    int h = a.size(), w = a[0].size();
    auto ans = vec2d(w, h, 0);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            ans[j][i] = a[i][j];
        }
    }
    return ans;
}

using P = pair<int, int>;

// 横に交互を計算
mint f(vector<vector<int>> a){
    int h = a.size(), w = a[0].size();
    auto mx = vec2d(h, 2, 0);
    vector<P> vp;
    for(int i = 0; i < h; i++){
        vector<vector<int>> v(2);
        for(int j = 0; j < w; j++){
            v[j%2].push_back(a[i][j]);
        }
        for(int p = 0; p < 2; p++) mx[i][p] = *max_element(v[p].begin(), v[p].end());
    }
    mint prod = mint(2).pow(h);
    vector<int> rem(h, 2);
    for(int i = 0; i < h; i++){
        for(int p = 0; p < 2; p++){
            vp.push_back({mx[i][p], i});
        }
    }
    vector<mint> inv = {mint(0), mint(1), mint(2).inv()};
    mint ans = 0;
    sort(vp.begin(), vp.end(), greater<P>());
    for(auto [x, i]: vp){
        prod *= inv[rem[i]];
        ans += x*prod;
        rem[i]--;
        prod *= rem[i];
        cout << x << ':' << prod << endl;
        print_vector(rem);
    }
    return ans;
}

// 縦横に交互を計算
mint g(vector<vector<int>> a){
    int h = a.size(), w = a[0].size();
    mint ans = 0;
    for(int x: {0, 1}){
        int mx = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                int p = (i+j)%2;
                p ^= x;
                if(p == 1) chmax(mx, a[i][j]);
            }
        }
        debug_value(mx)
        ans += mx;
    }
    return ans;
}

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

template<typename T>
class Compress{
    public:
    vector<T> data;
    int offset;
    Compress(vector<T> data_, int offset=0): offset(offset){
        data = data_;
        sort(begin(data), end(data));
        data.erase(unique(begin(data), end(data)), end(data));
    };
    int operator[](T x) {
        auto p = lower_bound(data.begin(), data.end(), x);
        assert(x == *p);
        return offset+(p-data.begin());
	}
    T inv(int x){
        return data[x-offset];
    }
    int size(){
        return data.size();
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    auto a = vec2d(h, w, 0);
    vector<int> v;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
            v.push_back(a[i][j]);
        }
    }
    auto cp = Compress<int>(v);
    int m = cp.size();
    vector<vector<P>> indices(m);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            indices[cp[a[i][j]]].push_back({i, j});
        }
    }
    mint ans = 0;
    UnionFind uf(h+w);
    int rem = h+w-1;
    mint n_all = mint(2).pow(h+w-1);
    for(int x = m-1; x >= 0; x--){
        for(auto [i, j]: indices[x]){
            if(!uf.findSet(i, j+h)){
                rem--;
                uf.unionSet(i, j+h);
            }
        }
        int diff = x == 0 ? cp.data[x] : cp.data[x]-cp.data[x-1];
        ans += (n_all-mint(2).pow(rem))*diff;
    }
    cout << ans << endl;
}
0