結果

問題 No.1767 BLUE to RED
ユーザー HaaHaa
提出日時 2021-11-26 22:42:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 1,811 bytes
コンパイル時間 1,745 ms
コンパイル使用メモリ 177,312 KB
実行使用メモリ 28,216 KB
最終ジャッジ日時 2023-09-12 05:17:26
合計ジャッジ時間 6,726 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 141 ms
15,364 KB
testcase_10 AC 171 ms
19,976 KB
testcase_11 AC 154 ms
15,696 KB
testcase_12 AC 143 ms
14,852 KB
testcase_13 AC 185 ms
20,672 KB
testcase_14 AC 266 ms
25,844 KB
testcase_15 AC 269 ms
24,960 KB
testcase_16 AC 274 ms
26,740 KB
testcase_17 AC 272 ms
28,216 KB
testcase_18 AC 271 ms
26,752 KB
testcase_19 AC 272 ms
27,096 KB
testcase_20 AC 270 ms
27,324 KB
testcase_21 AC 270 ms
25,024 KB
testcase_22 AC 270 ms
25,928 KB
testcase_23 AC 270 ms
25,728 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:61:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   61 |     for(auto [d,x]:q){
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;

struct Unionfind{
   vector<int> par, siz;
   Unionfind(int N):par(N), siz(N,1){
       for(int i=0;i<N;i++) par[i]=i;
   }
   int root(int x){
       while(par[x]!=x) x=par[x]=par[par[x]];
       return x;
   }
   void unite(int x, int y){
       x=root(x); y=root(y);
       if(x==y) return;
       if(siz[x]<siz[y]) swap(x,y);
       siz[x]+=siz[y];
       par[y]=x;
   }
   bool same(int x, int y){
       return root(x)==root(y);
   }
   int size(int x){
       return siz[root(x)];
   }
};

int main(){
    int n, m; cin >> n >> m;
    VI a(n), b(m);
    REP(i,n) cin >> a[i];
    REP(i,m) cin >> b[i];
    vector<P> p;
    REP(i,n) p.push_back({a[i],0});
    REP(i,m) p.push_back({b[i],1});
    sort(ALL(p));
    vector<P> q;
    REP(i,n+m-1){
        if(!(p[i].second==0&&p[i+1].second==0)){
            q.push_back({p[i+1].first-p[i].first,i});
        }
    }
    sort(ALL(q));
    Unionfind uf(n+m);
    VI f(n+m,0);
    REP(i,n+m){
        if(p[i].second==0)
            f[i]=1;
    }
    ll ans=0;
    for(auto [d,x]:q){
        int r1=uf.root(x), r2=uf.root(x+1);
        if(f[r1]&&f[r2])
            continue;
        else if(f[r1]||f[r2]){
            f[r1]=1;
            f[r2]=1;
            uf.unite(r1,r2);
            ans+=d;
        }
        else{
            uf.unite(r1,r2);
            ans+=d;
        }
    }
    cout << ans << endl;
    return 0;
}
0