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>
#define vi vector<int>
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
main() {
int n;
cin >> n;
vi v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
--v[i];
}
vi g = v;
sort(g.begin(),g.end());
// INVALID
for (int i = 0; i < n; i++)
if (g[i] < i) {
cout <<-1;
return 0;
}//
int ans = 0;
while(v.size()) {
int n = v.size();
for (int i = n-1; i >= 0; i --) {
if (v[i] == n-1) {
int j = i;
while (j < n-1) {
swap(v[j],v[j+1]);
ans++;
j++;
}
v.pop_back();
for (int i = 0; i < n-1; i++)
if (v[i] == n-1)
v[i]--;
break;
//cout << ans;
}
// cout <<"DS";
}
}
cout << ans;
}
Compilation message (stderr)
Main.cpp:12:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main() {
| ^
# | 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... |