이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200002;
int n;
int d[N_MAX];
int p[N_MAX];
set <int> s;
int BIT[N_MAX];
void update (int pos)
{
for(int i = pos; i >= 1; i -= i & -i)
BIT[i]++;
}
int query (int pos)
{
int ans = 0;
for(int i = pos; i <= n; i += i & -i)
ans += BIT[i];
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> d[i];
for(int i = 1; i <= n; i++)
s.insert(i);
for(int i = n; i >= 1; i--)
{
set <int> :: iterator it = s.upper_bound(d[i]);
if(it == s.begin())
{
cout << "-1\n";
return 0;
}
it--;
p[i] = *it;
s.erase(it);
}
ll ans = 0;
for(int i = 1; i <= n; i++)
{
ans += query(p[i]);
update(p[i]);
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |