This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define clz __builtin_clzll
#define ctz __builtin_ctzll
#define popcount __builtin_popcount
#define lg(x) (63 - clz(x))
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a)); a.resize(unique(all(a)) - a.begin());
}
const ll oo = (ll) 1e18, inf = (ll) 1e9, mod = (ll) 1e9 + 7;
const int mxn = (int) 1e6 + 5, S = (int) 450, S2 = (int) 450, lg = (int) 18;
void add(ll &x, ll y) {
x += y;
if (x >= mod) x -= mod;
}
void sub(ll &x, ll y) {
x -= y;
if (x < 0) x += mod;
}
int n;
int a[mxn];
namespace one {
bool one() {
return n <= 1e3;
}
const int mxn = (int) 1e3 + 5;
int f[mxn];
vector<int> cur[mxn];
void solve() {
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
cur[i].push_back(a[j]);
compress(cur[i]);
}
for (int i = 1; i <= n; ++i) {
f[i] = f[i-1] + 1;
for (int j = i-1; j >= 1; --j) {
if (a[j] > a[j+1]) break;
if (a[i] == a[j]) {
minimize(f[i], f[j-1] + 1);
continue;
}
int cnt = upper_bound(all(cur[j-1]), a[i] - 1) - lower_bound(all(cur[j-1]), a[j] + 1);
if (cnt != 0) break;
minimize(f[i], f[j-1] + 1);
}
}
cout << f[n];
return;
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
{
if (one::one()) return one::solve();
}
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "task"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message (stderr)
money.cpp: In function 'int main()':
money.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | 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... |