Submission #52518
ソースコード
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include<bits/stdc++.h> #define REP(i,n) for(long long i = 0;i < n;++i) #define FOR(i,a,b) for(long long i = a;i < b;++i) #define F first #define S second #define OUT(x) cout << x << "\n" using namespace std; using ll = long long ; using point = pair< int , int >; template < class T > class Segment { // 0-indexed // parent : (n - 1) / 2 // left : 2 * n + 1 // right : 2 * n + 2 int size; T inf; function< T(T, T) > f; vector< T > data; public : Segment( int size, T val, T inf, function< T(T, T) > f) : inf(inf), f(f) { int _n = 1; while (_n < size) { _n *= 2; } this -> size = _n; data.resize(_n * 2 - 1, inf); } // 値の更新 void update( int p, T v) { p += size - 1; data[p] = v; while (p > 0) { p = (p - 1) / 2; data[p] = f(data[2 * p + 1], data[2 * p + 2]); } return ; } void add( int p, T v) { p += size - 1; data[p] += v; while (p > 0) { p = (p - 1) / 2; data[p] = f(data[2 * p + 1], data[2 * p + 2]); } return ; } // [a, b)に対するクエリ // usage : query(a, b, 0, 0, n); T query( int a, int b, int p, int l, int r) { if (b <= l || r <= a) return inf; else if (a <= l && r <= b) return data[p]; else { T lv = query(a, b, 2 * p + 1, l, (l + r) / 2); T rv = query(a, b, 2 * p + 2, (l + r) / 2, r); return f(lv, rv); } } T query( int a, int b) { return query(a, b, 0, 0, size); } int lower_bound( int a, int b, T x, int p, int l, int r) { if (b <= l || r <= a) return -1; else if (p >= size - 1) return p - (size - 1); else { if (data[2 * p + 1] < x) return lower_bound(a, b, x - data[2 * p + 1], 2 * p + 2, (l + r) / 2, r); else return lower_bound(a, b, x, 2 * p + 1, l, (l + r) / 2); } } int lower_bound( int a, int b, T x) { return lower_bound(a, b, x, 0, 0, size); } }; using Pci = pair< char , int >; int main() { string s; int k; cin >> s >> k; Segment< Pci > stree(s.size(), Pci(CHAR_MAX, -1), Pci(CHAR_MAX, -1), [](Pci a, Pci b) { return min(a, b); }); Segment< int > flg(s.size(), 0, 0, []( int a, int b) { return a + b; }); REP (i, s.size()) { stree.update(i, Pci(s[i], i)); flg.update(i, 1); } string ans; REP (i, s.size()) { int idx = flg.lower_bound(0, s.size(), k + 1); fprintf (stderr, "idx : %d\n" , idx); if (idx == -1) idx = s.size() - 1; Pci ch = stree.query(0, idx + 1); ans.push_back(ch.F); flg.update(ch.S, 0); stree.update(ch.S, Pci(CHAR_MAX, -1)); k -= flg.query(0, ch.S); } OUT(ans); return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0963 - 人気のユーザ名 |
ユーザー名 | Wonder /*ei1741*/ |
投稿日時 | 2019-08-09 05:26:56 |
言語 | C++14 |
状態 | Accepted |
得点 | 14 |
ソースコード長 | 3046 Byte |
最大実行時間 | 349 ms |
最大メモリ使用量 | 8560 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 14 / 14 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 24 ms | 604 KB |
1
|
in2.txt | AC | 28 ms | 772 KB |
1
|
in3.txt | AC | 17 ms | 1844 KB |
1
|
in4.txt | AC | 21 ms | 1972 KB |
1
|
in5.txt | AC | 22 ms | 1944 KB |
1
|
in6.txt | AC | 17 ms | 2036 KB |
1
|
in7.txt | AC | 20 ms | 2004 KB |
1
|
in8.txt | AC | 14 ms | 2100 KB |
1
|
in9.txt | AC | 23 ms | 1936 KB |
1
|
in10.txt | AC | 17 ms | 440 KB |
1
|
in11.txt | AC | 17 ms | 540 KB |
1
|
in12.txt | AC | 21 ms | 512 KB |
1
|
in13.txt | AC | 20 ms | 476 KB |
1
|
in14.txt | AC | 23 ms | 572 KB |
1
|
in15.txt | AC | 20 ms | 412 KB |
1
|
in16.txt | AC | 18 ms | 632 KB |
1
|
in17.txt | AC | 168 ms | 3928 KB |
1
|
in18.txt | AC | 182 ms | 4072 KB |
1
|
in19.txt | AC | 176 ms | 4216 KB |
1
|
in20.txt | AC | 187 ms | 4328 KB |
1
|
in21.txt | AC | 322 ms | 7696 KB |
1
|
in22.txt | AC | 340 ms | 8064 KB |
1
|
in23.txt | AC | 349 ms | 8176 KB |
1
|
in24.txt | AC | 338 ms | 8280 KB |
1
|
in25.txt | AC | 29 ms | 1700 KB |
1
|
in26.txt | AC | 22 ms | 1788 KB |
1
|
in27.txt | AC | 21 ms | 1740 KB |
1
|
in28.txt | AC | 30 ms | 1676 KB |
1
|
in29.txt | AC | 17 ms | 1768 KB |
1
|
in30.txt | AC | 18 ms | 1812 KB |
1
|
in31.txt | AC | 232 ms | 8560 KB |
1
|
in32.txt | AC | 201 ms | 5440 KB |
1
|