#include<bits/stdc++.h>
/*
--> Programmed by katamori1310 <--
ᓀ‸ᓂ
Chillin with Blue Archive
*/
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define aris ""
using namespace std;
const int MAXN = 100000;
const int MOD = 1e9 + 7;
const int MAXP = 1e6;
const int INF = 1e18;
const int MODE = MOD - 2;
/*
I love Aris and Kei at the same time
☆ ☆ ☆ Pampakapam ☆ ☆ ☆
*/
inline void kei(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n;
int a[2 * MAXN + 5];
int pos[2 * MAXN + 5];
int st[8 * MAXN + 5];
vector<int>d[2 * MAXN + 5];
priority_queue<int>pq;
void update(int id, int l, int r, int i, int val){
if(l > i || r < i) return;
if(l == r){
st[id] = val;
return;
}
int mid = (l + r) / 2;
if(i <= mid) update(2 * id, l, mid, i, val);
else update(2 * id + 1, mid + 1, r, i, val);
st[id] = st[2 * id] + st[2 * id + 1];
}
int query(int id, int l, int r, int u, int v){
if(l > v || r < u) return 0;
if(l >= u && r <= v) return st[id];
int mid = (l + r) / 2;
int query1 = query(2 * id, l, mid, u, v);
int query2 = query(2 * id + 1, mid + 1, r, u, v);
return query1 + query2;
}
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
d[a[i]].push_back(i);
}
for(int i = n; i >= 1; i--){
for(auto it : d[i]){
pq.push(it);
}
if(pq.empty()){
cout << -1 << "\n";
return;
}
pos[i] = pq.top();
pq.pop();
}
int res = 0;
for(int i = 1; i <= n; i++){
res += i - query(1, 1, n, 1, pos[i]) - 1;
update(1, 1, n, pos[i], 1);
}
cout << res << "\n";
}
signed main(){
if(fopen(aris".inp", "r")){
freopen(aris".inp", "r", stdin);
freopen(aris".out", "w", stdout);
}
kei();
int T;
// cin >> T;
T = 1;
while(T--){
solve();
}
return 0;
}
// 눈‸눈
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.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(aris".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen(aris".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |