結果
問題 | No.145 yukiover |
ユーザー |
![]() |
提出日時 | 2015-02-06 00:51:13 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 5 ms / 5,000 ms |
コード長 | 2,072 bytes |
コンパイル時間 | 754 ms |
コンパイル使用メモリ | 77,256 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-23 09:27:09 |
合計ジャッジ時間 | 1,834 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <set> using namespace std; template<class T> class BinaryIndexedTree_1_indexed{ void init(const vector<T> &A){ for(int i=0; i<N; i++){ add(i+1, A[i]); } } public: vector<T> tree; int N; BinaryIndexedTree_1_indexed(const int n) : tree(n+1,0), N(n){ } BinaryIndexedTree_1_indexed(const vector<T> &A) : tree(A.size()+1,0), N(A.size()){ init(A); } //caution : position "i" must be 1-indexed void add(int i, const T x){ while(i <= N){ tree[i] += x; i += i & -i; } } //get sums [0,i] T get_sum(int i){ T ret=0; while(i>0){ ret += tree[i]; i -= i & -i; } return ret; } //get sums [from,to] T get_sums_range(const int from, const int to){ return get_sum(to) - get_sum(from-1); } //get at [i] T get_at(const int i){ return get_sum(i) - get_sum(i-1); } int lower_bound(T val){ if(val<=0) return 0; int x = 0; int k = 1; while((k<<1) <= N) k<<=1; for( ; k>0; k>>=1){ if( x+k <= N && tree[x+k] < val ){ val -= tree[x+k]; x += k; } } return x+1; } void print(){ for(int i=0; i<=N; i++){ cerr << tree[i] << " "; } cerr << endl; } }; int main(){ int N; cin >> N; string s; cin >> s; const string y = "yuki`"; vector<int> alph(27, 0); for(int i=0; i<N; i++){ alph[ s[i]-'`' ]++; } BinaryIndexedTree_1_indexed<int> BIT(alph); int ans = 0; int i = 0; while(N>0){ int a = BIT.get_at(y[i] - '`' +1); int b = BIT.get_sum(y[i] - '`' +1); if(a>0){ //s[i]と同じものが残っている BIT.add(y[i] - '`' +1, -1); N--; i++; }else if(N-b > 0){ //s[i]より大きいアルファベットが残ってる => 辞書順で大きいものが作れる //残ってるものの中で最小のアルファベットを探索 int pos = BIT.lower_bound(b+1); ans++; BIT.add(pos, -1); N--; i = 0; }else{ break; } } cout << ans << endl; return 0; }