結果
| 問題 |
No.1767 BLUE to RED
|
| コンテスト | |
| ユーザー |
Haa
|
| 提出日時 | 2021-11-26 22:42:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 279 ms / 2,000 ms |
| コード長 | 1,811 bytes |
| コンパイル時間 | 1,880 ms |
| コンパイル使用メモリ | 178,616 KB |
| 実行使用メモリ | 24,968 KB |
| 最終ジャッジ日時 | 2024-06-29 18:17:34 |
| 合計ジャッジ時間 | 6,678 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:61:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
61 | for(auto [d,x]:q){
| ^
ソースコード
#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;
}
Haa