結果

問題 No.145 yukiover
ユーザー koyumeishikoyumeishi
提出日時 2015-02-06 00:51:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,072 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 76,516 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 13:44:44
合計ジャッジ時間 1,783 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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