結果

問題 No.2884 Pieces on Squares
ユーザー pockynypockyny
提出日時 2024-09-14 16:55:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,367 bytes
コンパイル時間 1,124 ms
コンパイル使用メモリ 91,172 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-09-14 16:55:52
合計ジャッジ時間 4,274 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 35 ms
6,940 KB
testcase_04 AC 10 ms
6,944 KB
testcase_05 AC 34 ms
6,940 KB
testcase_06 AC 95 ms
7,296 KB
testcase_07 AC 90 ms
7,552 KB
testcase_08 AC 80 ms
7,040 KB
testcase_09 AC 47 ms
6,940 KB
testcase_10 AC 50 ms
6,944 KB
testcase_11 AC 120 ms
8,192 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 29 ms
6,940 KB
testcase_18 AC 20 ms
6,940 KB
testcase_19 WA -
testcase_20 AC 41 ms
6,944 KB
testcase_21 WA -
testcase_22 AC 100 ms
7,680 KB
testcase_23 AC 57 ms
6,944 KB
testcase_24 AC 90 ms
7,168 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 37 ms
6,944 KB
testcase_31 WA -
testcase_32 AC 74 ms
6,944 KB
testcase_33 AC 70 ms
6,940 KB
testcase_34 AC 4 ms
6,940 KB
testcase_35 AC 7 ms
6,944 KB
testcase_36 AC 6 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 33 ms
6,940 KB
testcase_46 AC 6 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 3 ms
6,944 KB
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
struct UF{
    vector<int> par,sz;
    vector<vector<int>> mergeTree;
    UF(int n, bool useMergeTree = false){
        sz.resize(n); par.resize(n);
        for(int i=0;i<n;i++) sz[i] = 1, par[i] = i;
        if(useMergeTree){
            sz.resize(2*n); par.resize(2*n);
            for(int i=n;i<2*n;i++) sz[i] = 1, par[i] = i;
            mergeTree.resize(2*n);
        }
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x] = find(par[x]); 
    }
    void unite(int x,int y){
        x = find(x); y = find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x,y);
        sz[y] += sz[x];
        par[x] = y;
    }
    bool same(int x,int y){return find(x)==find(y);}
    void merge(int child,int parent){
        //parentが親,merge過程を表す木などで使用
        child = find(child); parent = find(parent);
        if(child==parent) return;
        mergeTree[parent].push_back(child);
        sz[parent] += sz[child];
        par[child] = parent;
    }
};

#include <iostream>
#include <vector>
#include <set>

typedef long long ll;
using namespace std;
vector<int> H[5010],W[5010];
ll dp_mn[5010],dp_mx[5010],ndp_mn[5010],ndp_mx[5010],inf = 1000000000;
int a[200010],b[200010];
int main(){
    int i,j,h,w,n; cin >> h >> w >> n;
    UF ufh(h),ufw(w);
    for(i=0;i<n;i++){
        cin >> a[i] >> b[i]; a[i]--; b[i]--;
        H[a[i]].push_back(b[i]); W[b[i]].push_back(a[i]);
    }
    for(i=0;i<h;i++){
        for(j=1;j<H[i].size();j++) ufw.unite(H[i][j],H[i][0]);
    }
    for(i=0;i<w;i++){
        for(j=1;j<W[i].size();j++) ufh.unite(W[i][j],W[i][0]);
    }
    set<pair<int,int>> s;
    for(i=0;i<n;i++){
        s.insert({ufh.find(a[i]),ufw.find(b[i])});
    }
    for(i=0;i<=h;i++) dp_mx[i] = ndp_mx[i] = -inf, dp_mn[i] = ndp_mn[i] = inf;
    dp_mn[0] = dp_mx[0] = 0;
    // 黒 := h_c*w+ w_c*h - 2*h_c*w_c = h_c*w + (h - h_c*2)*w_c, (wの最小,最大を持てばOK)
    int _h = h,_w = w;
    for(auto [p,q]:s){
        int h_c = ufh.sz[p],w_c = ufw.sz[q];
        _h -= h_c; _w -= w_c;
        // cout << "s := " << h_c << " " << w_c << endl;
        for(i=0;i<=h;i++){
            if(i>=h_c){
                ndp_mx[i] = max(ndp_mx[i],dp_mx[i - h_c] + w_c);
                ndp_mn[i] = min(ndp_mn[i],dp_mn[i - h_c] + w_c);
            }
            ndp_mx[i] = max(ndp_mx[i],dp_mx[i]);
            ndp_mn[i] = min(ndp_mn[i],dp_mn[i]);
        }
        for(i=0;i<=h;i++){
            dp_mx[i] = ndp_mx[i]; ndp_mx[i] = -inf;
            dp_mn[i] = ndp_mn[i]; ndp_mn[i] = inf;
        }
    }
    ll ans = 0;
    for(i=0;i<=h;i++){
        // cout << i << " " << dp_mn[i] << " "  << dp_mx[i] << "\n";
        if(dp_mn[i]<=w) ans = max(ans,i*w + dp_mn[i]*h - dp_mn[i]*i*2);
        if(dp_mx[i]>=0) ans = max(ans,i*w + dp_mx[i]*h - dp_mx[i]*i*2);
    }
    cout << ans + _h*_w << "\n";
    // for(i=0;i<(1<<h);i++){
    //     for(j=0;j<(1<<w);j++){
    //         for(int k=0;k<n;k++){
    //             if((i>>a[k]&1)!=(j>>b[k]&1)) break;
    //             if(k==n - 1){
    //                 int len_i = __builtin_popcount(i);
    //                 int len_j = __builtin_popcount(j);
    //                 cout << "ok " << i << " " << j << " " << len_i << " " << len_j << endl;
    //             }
    //         }
    //     }
    // }
}
0