結果
問題 |
No.5004 Room Assignment
|
ユーザー |
|
提出日時 | 2021-11-29 18:06:52 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 246 ms / 5,000 ms |
コード長 | 1,389 bytes |
コンパイル時間 | 2,936 ms |
実行使用メモリ | 22,380 KB |
スコア | 96,099,360 |
平均クエリ数 | 7612.17 |
最終ジャッジ日時 | 2021-11-30 23:56:32 |
合計ジャッジ時間 | 32,864 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge12 |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 100 |
ソースコード
#include<bits/stdc++.h> using namespace std; 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)]; } }; int main(){ int T,R; cin >> T >> R; int p=0; UnionFind uf(5555); vector<int> S(5555); vector<int> ma(5555),mi(5555); for(int i=0;i<T;i++){ int n; cin >> n; vector<pair<int,int>> act; for(int j=0;j<n;j++){ p++; cin >> S[p]; ma[p]=S[p]; mi[p]=S[p]; for(int k=p-1;k>=1;k--){ if(uf.size(p)==R){break;} if(uf.findSet(k,p)){continue;} if(uf.size(k)+uf.size(p)>R){continue;} int rk=uf.root(k); int rp=uf.root(p); if(max(ma[rk],ma[rp])-min(mi[rk],mi[rp])>0){continue;} uf.unionSet(rk,rp); act.push_back({rk,rp}); ma[uf.root(rk)]=max(ma[rk],ma[rp]); mi[uf.root(rk)]=min(mi[rk],mi[rp]); } } cout << act.size() << '\n'; for(auto &nx : act){cout << nx.first << ' ' << nx.second << '\n';} fflush(stdout); } return 0; }