QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#385075 | #2177. Group Photo | Rikku_eq | 0 | 1ms | 3960kb | C++14 | 986b | 2024-04-10 15:15:20 | 2024-04-10 15:15:20 |
answer
#include <bits/stdc++.h>
#define N 5005
using namespace std;
typedef long long ll;
int n, a[N], cnt[N][N], f[N], bit[N];
void add (int x) { while (x<=n) { bit[x]++; x+=(x&(-x)); } }
int qry (int x) { int res=0; while (x) { res+=bit[x]; x-=(x&(-x)); } return res; }
int cal (int l, int r)
{
int len=r-l+1;
return cnt[1][r]-cnt[1][l-1]-cnt[l][r]+(len*(len-1)/2)-cnt[l][r];
}
int main ()
{
// freopen("0test.in", "r", stdin);
// freopen("0test.out", "w", stdout);
scanf("%d", &n);
for (int i=1; i<=n; i++) { scanf("%d", &a[i]); }
for (int l=1; l<=n; l++) {
fill(bit+1, bit+n+1, 0);
for (int r=l; r<=n; r++) {
add(a[r]);
cnt[l][r]=cnt[l][r-1]+r-l+1-qry(a[r]);
}
}
for (int i=1; i<=n; i++) {
f[i]=i*(i-1)/2;
for (int j=i; j>=1; j--) {
f[i]=min(f[i], f[j-1]+cal(j, i));
}
}
printf("%d\n", f[n]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 1ms
memory: 3872kb
input:
3 3 1 2
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
4 4 1 2 3
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
5 1 4 5 2 3
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
6 1 5 6 3 4 2
output:
2
result:
ok single line: '2'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3912kb
input:
7 1 7 4 5 2 6 3
output:
5
result:
ok single line: '5'
Test #6:
score: -5
Wrong Answer
time: 0ms
memory: 3960kb
input:
8 4 1 6 7 5 2 8 3
output:
6
result:
wrong answer 1st lines differ - expected: '7', found: '6'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%