// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "gen"
const int MOD = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18 + 27092008;
const int N = 1e6 + 5;
int n, a[N], f[N];
void init(void) {
cin >> n;
FOR(i, 1, n) cin >> a[i];
}
void process(void) {
memset(f, 0x3f, sizeof f);
f[0] = 0;
set<ii> fs;
set<int> s;
int j = 1;
FOR(i, 0, n) {
while(!fs.empty() && fs.begin()->se == i) fs.erase(*fs.begin());
if (!fs.empty()) f[i] = fs.begin()->fi;
if (f[i] == inf) continue;
maximize(j,i+2);
while(j <= n && a[j] >= a[j-1]) j++;
auto it = s.upper_bound(a[i+1]);
int val = (it == s.end() ? inf : *it);
int low = i + 1, high = j - 1, res = j - 1;
while(low <= high) {
int mid = (low + high) >> 1;
if (a[mid] <= val) res = mid, low = mid + 1;
else high = mid - 1;
}
s.insert(a[i+1]);
fs.insert(mp(f[i] + 1, res + 1));
}
cout << f[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
money.cpp: In function 'int main()':
money.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |