Submission #467130

#TimeUsernameProblemLanguageResultExecution timeMemory
467130aryan12Money (IZhO17_money)C++17
45 / 100
1581 ms4852 KiB
#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() {
	int n;
	cin >> n;
	int a[n + 1];
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> temp;
	int cur_min = a[1], cur_max = a[1];
	int prev = 1;
	int ans = 1;
	for(int i = 2; i <= n; i++) {
		if(a[i] < a[i - 1]) {
			ans++;
			for(int j = 0; j < temp.size(); j++) {
				if(temp[j] > cur_min && temp[j] < cur_max) {
					ans++;
					break; 
				}
			}
			for(int j = i - 1; j >= prev; j--) {
				temp.push_back(a[j]);
			}
			prev = i;
			cur_min = a[i];
			cur_max = a[i];
		}
		else {
			cur_max = max(cur_max, a[i]);
			cur_min = min(cur_min, a[i]);
			int f = 0;
			for(int j = 0; j < temp.size(); j++) {
				if(temp[j] > cur_min && temp[j] < cur_max) {
					ans++;
					f = 1;
					break;
				}
			}
			if(f == 1) {
				for(int j = i - 1; j >= prev; j--) {
					temp.push_back(a[j]);
				}
				prev = i;
				cur_min = a[i];
				cur_max = a[i];
			}
		}
	}
	for(int j = 0; j < temp.size(); j++) {
		if(temp[j] > cur_min && temp[j] < cur_max) {
			ans++;
			break; 
		}
	}

	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 (stderr)

money.cpp: In function 'void Solve()':
money.cpp:42:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |    for(int j = 0; j < temp.size(); j++) {
      |                   ~~^~~~~~~~~~~~~
money.cpp:59:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |    for(int j = 0; j < temp.size(); j++) {
      |                   ~~^~~~~~~~~~~~~
money.cpp:76:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |  for(int j = 0; j < temp.size(); j++) {
      |                 ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...