#include<bits/stdc++.h>
using namespace std;
int n, i, j, k, A[300010], x;
vector<int>comp, v, V;
long long X[300010], Y[300010], ans=9e18, inv, add;
struct seg
{
int seg[1200010];
void update(int id, int s, int e, int x, int v)
{
if(e<x||x<s)return;
if(s==e)
{
seg[id]+=v;
return;
}
int m=s+e>>1;
update(id*2, s, m, x, v);update(id*2+1, m+1, e, x, v);
seg[id]=seg[id*2]+seg[id*2+1];
}
int get(int id, int s, int e, int l, int r)
{
if(e<l||r<s)return 0;
if(l<=s&&e<=r)return seg[id];
int m=s+e>>1;
return get(id*2, s, m, l, r)+get(id*2+1, m+1, e, l, r);
}
}t1, t2;
main()
{
for(scanf("%d", &n);i++<n;)scanf("%d", &A[i]);
for(i=0;i++<n;)comp.push_back(A[i]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(i=0;i++<n;)A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;
x=comp.size();
for(i=0;i++<n;)
{
if(A[i]==x)v.push_back(i);
else V.push_back(A[i]);
}
for(i=0;i<v.size();i++)
{
X[1]--;Y[1]+=(v[i]-i);
X[v[i]-i]+=2;Y[v[i]-i]-=2*(v[i]-i);
}
for(i=1;i<=n+1-v.size();i++)X[i]+=X[i-1],Y[i]+=Y[i-1];
for(auto p:V)
{
inv+=t2.get(1, 1, x, 1, p-1);
t2.update(1, 1, x, p, 1);
}
for(i=1;i<=n+1-v.size();i++)
{
long long res=X[i]*i+Y[i];
if(i>=2)
{
inv-=t2.get(1, 1, x, V[i-2]+1, x);
t2.update(1, 1, x, V[i-2], -1);
add+=t1.get(1, 1, x, V[i-2]+1, x);
t1.update(1, 1, x, V[i-2], 1);
}
ans=min(ans, res+add+inv);
}
cout<<ans;
}
Compilation message
growing.cpp: In member function 'void seg::update(int, int, int, int, int)':
growing.cpp:17:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int m=s+e>>1;
| ~^~
growing.cpp: In member function 'int seg::get(int, int, int, int, int)':
growing.cpp:25:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int m=s+e>>1;
| ~^~
growing.cpp: At global scope:
growing.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main()
| ^~~~
growing.cpp: In function 'int main()':
growing.cpp:42:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(i=0;i<v.size();i++)
| ~^~~~~~~~~
growing.cpp:47:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(i=1;i<=n+1-v.size();i++)X[i]+=X[i-1],Y[i]+=Y[i-1];
| ~^~~~~~~~~~~~~~
growing.cpp:53:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(i=1;i<=n+1-v.size();i++)
| ~^~~~~~~~~~~~~~
growing.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | for(scanf("%d", &n);i++<n;)scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~
growing.cpp:31:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | for(scanf("%d", &n);i++<n;)scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
2252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |