Submission #68569
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> #include <vector> using namespace std; struct UnionFind{ int n; vector< int > data; UnionFind( int sz) : n(sz), data(sz, -1){} bool unite( int x, int y){ x = find(x), y = find(y); if (x == y) return ( false ); if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return ( true ); } int find( int k){ if (data[k] < 0) return (k); return (data[k] = find(data[k])); } int size( int k){ return (-data[find(k)]); } bool same( int x, int y){ return (find(x) == find(y)); } }; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n, q; cin >> n >> q; vector< int > res(q), x(q); vector< bool > white(n, true ); for ( int i = 0; i < q; ++i){ cin >> x[i]; x[i]--; white[x[i]] = false ; } UnionFind uf(n); int maxlen = 1; for ( int i = 0; i < n - 1; ++i){ if (white[i] && white[i + 1]){ uf.unite(i, i + 1); maxlen = max(maxlen, uf.size(i)); } } for ( int i = q - 1; i >= 0; --i){ res[i] = maxlen; white[x[i]] = true ; if (white[x[i]]){ if (x[i] != 0 && white[x[i] - 1]){ uf.unite(x[i] - 1, x[i]); } if (x[i] != n - 1 && white[x[i] + 1]){ uf.unite(x[i], x[i] + 1); } maxlen = max(maxlen, uf.size(x[i])); } } for ( int i = 0; i < q; ++i){ cout << res[i] << '\n' ; } return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 1528 - Monochrome Balls |
ユーザー名 | ei1903 |
投稿日時 | 2021-09-23 16:36:25 |
言語 | C++17 |
状態 | Accepted |
得点 | 4 |
ソースコード長 | 1666 Byte |
最大実行時間 | 69 ms |
最大メモリ使用量 | 22780 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 4 / 4 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01.txt | AC | 27 ms | 604 KB |
1
|
in02.txt | AC | 23 ms | 660 KB |
1
|
in03.txt | AC | 21 ms | 488 KB |
1
|
in04.txt | AC | 24 ms | 544 KB |
1
|
in05.txt | AC | 20 ms | 608 KB |
1
|
in06.txt | AC | 29 ms | 1432 KB |
1
|
in07.txt | AC | 25 ms | 900 KB |
1
|
in08.txt | AC | 27 ms | 2424 KB |
1
|
in09.txt | AC | 31 ms | 2044 KB |
1
|
in10.txt | AC | 48 ms | 3648 KB |
1
|
in11.txt | AC | 33 ms | 3632 KB |
1
|
in12.txt | AC | 60 ms | 6108 KB |
1
|
in13.txt | AC | 42 ms | 5016 KB |
1
|
in14.txt | AC | 44 ms | 5164 KB |
1
|
in15.txt | AC | 39 ms | 4872 KB |
1
|
in16.txt | AC | 57 ms | 7460 KB |
1
|
in17.txt | AC | 48 ms | 6688 KB |
1
|
in18.txt | AC | 56 ms | 8700 KB |
1
|
in19.txt | AC | 68 ms | 9796 KB |
1
|
in20.txt | AC | 69 ms | 10416 KB |
1
|
in21.txt | AC | 59 ms | 11168 KB |
1
|
in22.txt | AC | 68 ms | 12040 KB |
1
|
in23.txt | AC | 59 ms | 12912 KB |
1
|
in24.txt | AC | 61 ms | 13656 KB |
1
|
in25.txt | AC | 61 ms | 15684 KB |
1
|
in26.txt | AC | 55 ms | 17588 KB |
1
|
in27.txt | AC | 54 ms | 19620 KB |
1
|
in28.txt | AC | 55 ms | 21520 KB |
1
|
in29.txt | AC | 52 ms | 22780 KB |
1
|
in30.txt | AC | 32 ms | 20840 KB |
1
|
in31.txt | AC | 25 ms | 19360 KB |
1
|
sample.txt | AC | 19 ms | 19312 KB |
1
|