This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e5+5;
int bit[mxn];
int a[mxn];
vector<int> st;
int ans=0;
int L,R;
int n;
void update(int pos,int val){
for(;pos<mxn;pos+=(pos&-pos)){
bit[pos]+=val;
}
}
int query(int pos){
int ans=0;
for(;pos>0;pos-=(pos&-pos)){
ans+=bit[pos];
}
return ans;
}
void init(){
for(int i=0;i<n;i++){
update(a[i],1);
}
}
void upd(int l,int r){
while(L>l){
L--;
update(a[L],1);
}
while(R<r){
R++;
update(a[R],1);
}
while(L<l){
update(a[L],-1);
L++;
}
while(R>r){
update(a[R],-1);
R--;
}
}
void go(int l,int r,int tl,int tr){
if(l>r or tl>tr){
return;
}
int mid=(l+r)/2;
int mx=-1e9;
int node=max(tl,st[mid]);
for(int i=max(tl,st[mid]);i<=tr;i++){
upd(st[mid],i);
int val=query(a[st[mid]]-1)-query(a[i]);
if(val>mx){
mx=val;
node=i;
}
}
ans=max(ans,mx);
go(mid+1,r,node,tr);
go(l,mid-1,tl,node);
}
int main() {_
cin>>n;
map<int,int> mp;
for(int i=0;i<n;i++){
cin>>a[i];
mp[a[i]]++;
}
ll cnt=1;
for(auto &v:mp){
v.s=cnt++;
}
for(int i=0;i<n;i++){
a[i]=mp[a[i]];
}
cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
cnt+=(a[i]<a[j]);
}
}
if(cnt==0){
cout<<1<<'\n';
return 0;
}
L=0,R=n-1;
init();
for(int i=0;i<n;i++){
if(st.empty() or a[st.back()]<a[i]){
st.push_back(i);
}
}
go(0,(int)st.size()-1,0,n-1);
cout<<max(0ll,cnt-(2*ans+1))<<'\n';
return 0;
}
//maybe its multiset not set
Compilation message (stderr)
2014_ho_t5.cpp: In function 'void setIO(std::string)':
2014_ho_t5.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |