結果

問題 No.145 yukiover
ユーザー koyumeishikoyumeishi
提出日時 2015-02-06 00:40:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,074 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 76,432 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-05 13:28:32
合計ジャッジ時間 1,834 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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(N-b > 0){	//s[i]より大きいアルファベットが残ってる => 辞書順で大きいものが作れる
			//残ってるものの中で最小のアルファベットを探索
			int pos = BIT.lower_bound(b+1);

			ans++;
			BIT.add(pos, -1);
			N--;

			i = 0;
		}else if(a>=0){	//s[i]と同じものが残っている
			BIT.add(y[i] - '`' +1, -1);
			N--;

			i++;
		}else{
			break;
		}

	}

	cout << ans << endl;

	return 0;
}
0