답안 #467117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
467117 2021-08-21T18:11:02 Z aryan12 Money (IZhO17_money) C++17
0 / 100
1 ms 332 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

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() {
	int n;
	cin >> n;
	int a[n + 1];
	map<int, int> cc;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cc[a[i]] = 1;
	}
	int temp = 1;
	for(auto i: cc) {
		cc[i.first] = temp++;
	}
	for(int i = 1; i <= n; i++) {
		a[i] = cc[a[i]];
	}
	vector<array<int, 3> > breakPoints;
	breakPoints.push_back({1, 0, 0});
	int cur_min = a[1], cur_max = a[1];
	for(int i = 2; i <= n; i++) {
		if(a[i] < a[i - 1]) {
			breakPoints.push_back({i, cur_min, cur_max});
			cur_min = INT_MAX;
			cur_max = -1;
		}
		cur_max = max(cur_max, a[i]);
		cur_min = min(cur_min, a[i]);
	}
	breakPoints.push_back({n, cur_min, cur_max});
	/*for(int i = 0; i < breakPoints.size(); i++) {
		cout << breakPoints[i][0] << " " << breakPoints[i][1] << " " << breakPoints[i][2] << "\n";
	}*/
	int pos = 1;
	int ans = 0, prev = 0;
	for(int i = 1; i <= n; i++) {
		if(pos != breakPoints.size() && breakPoints[pos][0] == i) {
			ans++;
			int cur = Query(breakPoints[pos][2]) - Query(breakPoints[pos][1] - 1);
			//cout << "prev = " << prev << ", cur = " << cur << "\n";
			if(cur + prev == breakPoints[pos][0] - breakPoints[pos - 1][0]) {
				//good
			}
			else {
				ans++;
			}
			pos++;
			prev = Query(breakPoints[pos][2]) - Query(breakPoints[pos][2] - 1);
			prev += Query(breakPoints[pos][1]) - Query(breakPoints[pos][1] - 1);
		}
		Update(a[i], 1);
	}
	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;
}

Compilation message

money.cpp: In function 'void Solve()':
money.cpp:61:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   if(pos != breakPoints.size() && breakPoints[pos][0] == i) {
      |      ~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -