結果
| 問題 |
No.1438 Broken Drawers
|
| コンテスト | |
| ユーザー |
itohdak
|
| 提出日時 | 2021-03-26 21:28:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 107 ms / 2,000 ms |
| コード長 | 1,817 bytes |
| コンパイル時間 | 1,991 ms |
| コンパイル使用メモリ | 201,080 KB |
| 最終ジャッジ日時 | 2025-01-19 21:53:41 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}
#define MAX_N 200005
class UnionFind
{
public:
int par[MAX_N];
int depth[MAX_N];
int nGroup[MAX_N];
UnionFind(int n) {
init(n);
}
void init(int n) {
for(int i=0; i<n; i++) {
par[i] = i;
depth[i] = 0;
nGroup[i] = 1;
}
}
int root(int x) {
if(par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
bool same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if(x == y) return;
if(depth[x] < depth[y]) {
par[x] = y;
nGroup[y] += nGroup[x];
nGroup[x] = 0;
} else {
par[y] = x;
nGroup[x] += nGroup[y];
nGroup[y] = 0;
if(depth[x] == depth[y])
depth[x]++;
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> A(n);
rep(i, n) {
cin >> A[i];
A[i]--;
}
UnionFind uf(n);
rep(i, n-1) {
if(A[i+1] < A[i]) {
uf.unite(A[i], A[i+1]);
}
}
set<int> st;
int ans = 0;
rep(i, n) {
if(st.count(uf.root(i))) continue;
ans += (uf.nGroup[uf.root(i)]+1)/2;
st.insert(uf.root(i));
}
cout << ans << endk;
return 0;
}
itohdak