# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
583483 | AngusWong | Group Photo (JOI21_ho_t3) | C++17 | 5012 ms | 320 KiB |
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>
using namespace std;
int n, a[5001], p, pos[5001], t, dp[5001], bit[5001];
vector<int> v;
void upd(int x){
for (int i=x; i<=n; i+=(i&(-i))) bit[i]++;
}
int qry(int x){
int ans=0;
for (int i=x; i; i-=(i&(-i))) ans+=bit[i];
return ans;
}
int main() {
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i];
for (int i=1; i<=n; i++){
dp[i]=2e9;
for (int j=1; j<=i; j++){
p=1;
v.clear();
v.push_back(-1);
for (int k=1; k<=n; k++) bit[k]=0;
for (int k=i; k>=j; k--) pos[k]=p++;
for (int k=1; k<=n; k++){
if (a[k]>i) pos[a[k]]=p++;
if (a[k]>=j) v.push_back(a[k]);
}
t=0;
for (int k=1; k<v.size(); k++){
t+=qry(n)-qry(pos[v[k]]);
upd(pos[v[k]]);
}
dp[i]=min(dp[i], dp[j-1]+t);
}
}
cout << dp[n] << "\n";
}
Compilation message (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... |