제출 #1335791

#제출 시각아이디문제언어결과실행 시간메모리
1335791nguyenkhangninh99Money (IZhO17_money)C++20
100 / 100
272 ms16080 KiB

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 1e6 + 5;
int bit[maxn];
void update(int p, int v){
    for(; p < maxn; p += p & -p) bit[p] += v;
}
int get(int p){
    int res = 0;
    for(; p; p -= p & -p) res += bit[p];
    return res;
}
int walk(int val){
	int ans = 0;
	for(int i = 20; i >= 0; i--){
        if(ans + (1 << i) < maxn && bit[ans + (1 << i)] < val){
            ans += (1 << i);
            val -= bit[ans];
        }
    }
	return ans + 1;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
    vector<int> a(n + 1);
	for(int i = 1; i <= n; i++){
        cin >> a[i];
        update(a[i], 1);
	}
	int ans = 1, ok = 0;
	for(int i = n - 1; i >= 1; i--){
		update(a[i + 1], -1);
		if(a[i] != a[i + 1]){
		    if((walk(get(a[i + 1] - 1)) != a[i])){ //a[i] phải bé hơn a[i + 1] nếu muốn chung
		        ans++; 
                ok = 0;
                continue;
		    }
			if(!ok) ok = 1;
			else if(get(a[i + 1]) > get(a[i + 1] - 1)){
                ans++; //nếu có [b < c] mà giá trị khác bằng c ở bên trái. phải cắt
                ok = 0;
            }
		}
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...