답안 #467142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
467142 2021-08-21T19:11:46 Z aryan12 Money (IZhO17_money) C++17
0 / 100
7 ms 8140 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e6 + 5;
int bit[N];

//3 5 4 6
//4 5 6 3
//3 4 5 4

void Update(int pos, int val) {
	for(int i = pos; i < N; i += (i & (-i))) {
		bit[i] += val;
	}
}

int Query(int pos) {
	int ans = 0;
	for(int i = pos; i > 0; i -= (i & (-i))) {
		ans += bit[i];
	}
	return ans;
}

void Solve() {
	for(int i = 0; i < N; i++) {
		bit[i] = 0;
	}
	int n;
	cin >> n;
	int a[n + 1];
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int ans = 1, prev = 1;
	int cur_low = a[1], cur_high = a[1];
	int cnt_low = 1, cnt_high = 1;
	Update(a[1], 1);
	for(int i = 2; i <= n; i++) {
		if(a[i] > cur_high) {
			cnt_high = 1;
		}
		else if(a[i] == cur_high) {
			cnt_high++;
		}
		if(a[i] < cur_low) {
			cnt_low = 1;
		}
		else if(a[i] == cur_low) {
			cnt_low++;
		}
		cur_low = min(cur_low, a[i]);
		cur_high = max(cur_high, a[i]);
		Update(a[i], 1);
		if(a[i - 1] > a[i]) {
			ans++; //has to be a breaking point
			cur_low = a[i];
			cur_high = a[i];
			cnt_low = 1;
			cnt_high = 1;
			prev = i;
		}
		else {
			if(cur_high - cur_low >= 2) { //possible here only
				int should_be = i - prev + 1;
				should_be -= (cnt_high + cnt_low);
				int real = Query(cur_high - 1) - Query(cur_low);
				//cout << "should_be = " << should_be << ", real = " << real << "\n";
				//cout << "prev = " << prev << ", i = " << i << "\n";
				//cout << "cnt_low = " << cnt_low << ", cnt_high = " << cnt_high << "\n";
				if(real > should_be) {
					prev = i;
					cnt_low = 1;
					cnt_high = 1;
					cur_low = a[i];
					cur_high = a[i];
					ans++;
				}
			}
		}
		//cout << "i = " << i << ", ans = " << ans << "\n";
	}
	cout << ans << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 8140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 8140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 8140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 8140 KB Output isn't correct
2 Halted 0 ms 0 KB -