# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
697513 | borisAngelov | Cryptography (NOI20_crypto) | C++17 | 168 ms | 17784 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int maxn = 300005;
const long long mod = 1000000007;
int n;
int a[maxn];
void read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
}
void compress()
{
vector<int> to_compress;
for (int i = 1; i <= n; ++i)
{
to_compress.push_back(a[i]);
}
sort(to_compress.begin(), to_compress.end());
unordered_map<int, int> compressed_values;
for (int i = 0; i < to_compress.size(); ++i)
{
compressed_values[to_compress[i]] = i + 1;
}
for (int i = 1; i <= n; ++i)
{
a[i] = compressed_values[a[i]];
}
}
int fenwick[maxn];
void update(int pos, int val)
{
while (pos <= n)
{
fenwick[pos] += val;
pos += (pos & (-pos));
}
}
int query(int pos)
{
int result = 0;
while (pos >= 1)
{
result += fenwick[pos];
pos -= (pos & (-pos));
}
return result;
}
long long factoriels[maxn];
void solve()
{
factoriels[1] = 1;
for (int i = 2; i <= n; ++i)
{
factoriels[i] = (factoriels[i - 1] * (1LL * i)) % mod;
}
for (int i = 1; i <= n; ++i)
{
update(a[i], +1);
}
long long ans = 0;
for (int i = 1; i <= n; ++i)
{
long long cnt_smaller = query(a[i] - 1);
ans = (ans + cnt_smaller * factoriels[n - i]) % mod;
//cout << i << ' ' << a[i] << ' ' << cnt_smaller << ' ' << factoriels[n - i] << endl;
update(a[i], -1);
}
ans = (ans + 1) % mod;
cout << ans << endl;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
read();
compress();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |