結果

問題 No.2884 Pieces on Squares
ユーザー pockyny
提出日時 2024-09-14 16:41:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,846 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 88,532 KB
最終ジャッジ日時 2025-02-24 08:36:00
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 34 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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)
    for(auto [p,q]:s){
        int h_c = ufh.sz[p],w_c = ufw.sz[q];
        // 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 << "\n";
}
0