이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define fur(i, a, b) for(ll i = a; i <= (ll) b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll) b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define nl "\n"
const ll mod = 1e9L + 7;
const ll mxn = 3e5L + 5;
ll n;
pl p[mxn];
ll fact[mxn];
ll mul(const ll &a, const ll &b) {
ll c = (a % mod) * (b % mod);
c %= mod;
if(c < 0)
c += mod;
return c;
}
ll sum(const ll &a, const ll &b) {
ll c = (a % mod) + (b % mod);
c %= mod;
if(c < 0)
c += mod;
return c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fur(i, 1, n) {
cin >> p[i].fr;
p[i].sc = i;
}
sort(p + 1, p + n + 1);
ll a[n + 1];
fur(i, 1, n) {
a[p[i].sc] = i;
}
fact[0] = fact[1] = 1;
fur(i, 2, n) {
fact[i] = mul(fact[i - 1], i);
}
ll dp[n + 1] = {};
ll res =0;
fur(i, 1, n) {
ll smaller = a[i] - 1;
fur(j, 1, i - 1) {
if(a[j] < a[i]) {
--smaller;
}
}
dp[i] = mul(smaller, fact[n - i]);
res = sum(res, dp[i]);
}
res = sum(res, 1);
cout << res << nl;
}
# | 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... |