Submission #52493
ソースコード
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 125 126 127 128 | #include <bits/stdc++.h> using namespace std; #define FOR(i,n) for(int i=0;i<n;i++) #define why(n,x) int n;while(cin >>n,n!=x) #define iFOR(i,x,n)for(int i=x;i<n;i++) #define fin <<'\n' #define __ <<" "<< #define ___ <<" " #define bash push_back #define ALL(x) x.begin(),x.end() #define SWAP(a,b) ((a != b) && (a += b,b = a - b,a -= b)) //#define int long long typedef long long ll; typedef pair < int , int > pii; typedef vector < int > vit; typedef map < string , int > mstit; typedef vector < pii > vpi ; typedef greater < pii > gpi; typedef priority_queue < pii , vpi , gpi > dijk ; static const signed int INF = 0x3f3f3f3f; static const signed long long LINF = 0x3f3f3f3f3f3f3f3fLL; static const signed int SMOD = 1000000007; static const signed int NMOD = 1000000009; static const signed int dx[]={1,0,-1,0,1,1,-1,-1}; static const signed int dy[]={0,-1,0,1,-1,1,-1,1}; bool inside( int x, int y, int w, int h){ return (x>=0 && y>=0 && x<w && y<h);} template < class T>T abs (T &x){ return x<0 ? -x : x;} template < class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template < class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int qp( int a,ll b, int mo){ int ans=1; do { if (b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;} while (b>>=1); return ans;} int gcd( int a, int b){ return b?gcd(b,a%b):a;} template < typename Monoid > struct SegmentTree { using F = function< Monoid(Monoid, Monoid) >; int sz; vector< Monoid > seg; const F f; const Monoid M1; SegmentTree( int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set( int k, const Monoid &x) { seg[k + sz] = x; } void build() { for ( int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update( int k, const Monoid &x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query( int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[]( const int &k) const { return seg[k + sz]; } }; struct Moji{ char m; int id; Moji( char a= 'z' +1, int b=-1){ m = a,id = b;}; }; Moji MMAX; inline void solve(){ string s,ans; int k; cin >> s; cin >> k; int n = s.size(); SegmentTree<Moji> seg(n, [](Moji a, Moji b){ if (a.m==b.m) return (a.id<b.id?a:b); else return (a.m<b.m?a:b);},MMAX); SegmentTree< int > bit(n, []( int a, int b){ return a+b;},0); for ( int i = 0; i < n; i++){ seg.set(i,Moji(s[i],i)); } seg.build(); for ( int i = 0; i < n && k > 0; i++) { int ok, ng, mid; ok = 0, ng = n; while ( ng - ok > 1) { mid = ( ng + ok ) >> 1; if ( mid - bit.query(0,mid+1) <= k) ok = mid; else ng = mid; } Moji m = seg.query(0, ok+1); ans += m.m; k -= m.id - bit.query(0, m.id); bit.update(m.id, 1); seg.update(m.id, MMAX); } for ( int i = 0; i < n; i++){ if (seg[i].m != MMAX.m) ans += seg[i].m; } cout << ans fin; } struct xyz{ xyz(){ cin.tie(0), ios::sync_with_stdio( false ); cout <<fixed<<setprecision(12);};}xyzzy; signed main(){solve(); return 0;} |
ステータス
項目 | データ |
---|---|
問題 | 0963 - 人気のユーザ名 |
ユーザー名 | ei1821 |
投稿日時 | 2019-08-05 22:09:41 |
言語 | C++14 |
状態 | Accepted |
得点 | 14 |
ソースコード長 | 3779 Byte |
最大実行時間 | 195 ms |
最大メモリ使用量 | 8384 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 14 / 14 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 22 ms | 476 KB |
1
|
in2.txt | AC | 19 ms | 756 KB |
1
|
in3.txt | AC | 24 ms | 1760 KB |
1
|
in4.txt | AC | 17 ms | 1944 KB |
1
|
in5.txt | AC | 16 ms | 1896 KB |
1
|
in6.txt | AC | 17 ms | 1852 KB |
1
|
in7.txt | AC | 21 ms | 1936 KB |
1
|
in8.txt | AC | 21 ms | 2016 KB |
1
|
in9.txt | AC | 22 ms | 2100 KB |
1
|
in10.txt | AC | 20 ms | 556 KB |
1
|
in11.txt | AC | 22 ms | 512 KB |
1
|
in12.txt | AC | 19 ms | 468 KB |
1
|
in13.txt | AC | 20 ms | 420 KB |
1
|
in14.txt | AC | 18 ms | 504 KB |
1
|
in15.txt | AC | 22 ms | 584 KB |
1
|
in16.txt | AC | 18 ms | 544 KB |
1
|
in17.txt | AC | 24 ms | 4208 KB |
1
|
in18.txt | AC | 24 ms | 4204 KB |
1
|
in19.txt | AC | 24 ms | 4336 KB |
1
|
in20.txt | AC | 42 ms | 4300 KB |
1
|
in21.txt | AC | 195 ms | 7772 KB |
1
|
in22.txt | AC | 30 ms | 7976 KB |
1
|
in23.txt | AC | 29 ms | 8180 KB |
1
|
in24.txt | AC | 29 ms | 8384 KB |
1
|
in25.txt | AC | 18 ms | 1792 KB |
1
|
in26.txt | AC | 24 ms | 1620 KB |
1
|
in27.txt | AC | 20 ms | 1692 KB |
1
|
in28.txt | AC | 22 ms | 1748 KB |
1
|
in29.txt | AC | 20 ms | 1820 KB |
1
|
in30.txt | AC | 14 ms | 1708 KB |
1
|
in31.txt | AC | 32 ms | 8304 KB |
1
|
in32.txt | AC | 38 ms | 5428 KB |
1
|