結果

問題 No.1438 Broken Drawers
ユーザー itohdakitohdak
提出日時 2021-03-26 21:28:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 1,817 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 207,060 KB
実行使用メモリ 15,392 KB
最終ジャッジ日時 2023-08-19 07:04:41
合計ジャッジ時間 4,853 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 108 ms
15,392 KB
testcase_07 AC 22 ms
6,144 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 26 ms
6,224 KB
testcase_13 AC 43 ms
7,900 KB
testcase_14 AC 17 ms
5,184 KB
testcase_15 AC 70 ms
10,280 KB
testcase_16 AC 68 ms
10,060 KB
testcase_17 AC 72 ms
10,344 KB
testcase_18 AC 73 ms
10,608 KB
testcase_19 AC 74 ms
10,628 KB
testcase_20 AC 76 ms
10,676 KB
testcase_21 AC 74 ms
10,440 KB
testcase_22 AC 76 ms
10,756 KB
testcase_23 AC 76 ms
10,908 KB
testcase_24 AC 77 ms
10,904 KB
testcase_25 AC 77 ms
10,916 KB
testcase_26 AC 77 ms
10,952 KB
testcase_27 AC 77 ms
10,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0