#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<=(int)b;i++)
#define FORD(i,a,b) for(int i=a;i>=(int)b;i--)
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define BIT(mask,i) ((mask>>(i))&1)
#define MASK(a) (1LL<=((a)))
#define uni(v) sort(all(v)); v.resize(unique(all(v)) - v.begin())
#define pii pair <int, int>
#define vi vector <int>
#define vl vector <ll>
template <class A,class B>
bool maximize(A &a, const B b)
{
if(a < b){ a = b; return 1;} return 0;
}
template <class A,class B>
bool minimize(A &a, const B b)
{
if(a > b){ a = b; return 1;} return 0;
}
const int maxn = 1e6 + 5;
int n;
int a[maxn];
namespace sub3
{
vector <int> sorted[maxn];
int dp[maxn];
void solve()
{
for(int i = 1; i <= n; i++)
{
sorted[i] = sorted[i - 1];
sorted[i].push_back(a[i]);
sort(all(sorted[i]));
}
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i; j >= 1; j--)
{
if(j != i && a[j] > a[j + 1]) break;
if(sorted[j - 1].empty() || sorted[j - 1][0] >= a[i] || sorted[j - 1].back() <= a[j]
|| sorted[j - 1][upper_bound(all(sorted[j - 1]), a[j]) - sorted[j - 1].begin()] >= a[i])
{
minimize(dp[i], dp[j - 1] + 1);
}
}
}
cout << dp[n];
}
}
namespace sub4
{
int le[maxn], ri[maxn], dp[maxn];
void solve()
{
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
ri[0] = 2e9;
set <int> se;
for(int i = 1; i <= n; i++)
{
le[i] = (a[i] >= a[i - 1] ? le[i - 1] : i);
}
for(int i = 1; i < n; i++)
{
se.insert(a[i]);
ri[i] = *se.upper_bound(a[i + 1]);
}
for(int i = 1; i <= n; i++)
{
int l = le[i], r = i, pos = i;
while(l <= r)
{
int mid = (l + r) >> 1;
if(ri[mid - 1] >= a[i]) pos = mid, r = mid - 1;
else l = mid + 1;
}
dp[i] = dp[pos - 1] + 1;
}
cout << dp[n];
}
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
// return sub3::solve();
return sub4::solve();
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define kieuoanh "kieuoanh"
if(fopen(kieuoanh".inp","r"))
{
freopen(kieuoanh".inp","r",stdin);
freopen(kieuoanh".out","w",stdout);
}
int tst = 1;
// cin >> tst;
while(tst--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
money.cpp: In function 'int main()':
money.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(kieuoanh".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(kieuoanh".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... |