QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#89940#5507. Investorsinstallb#WA 10ms4000kbC++201.7kb2023-03-21 20:23:122023-03-21 20:23:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-21 20:23:14]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:4000kb
  • [2023-03-21 20:23:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 6005;
#define M N

int inv[N][N]; // number of inversions in [l,r]
int n,k,a[N],b[N];

int tr[N];
inline int lowbit(int x){ return x & (-x); }
inline void modify(int x,int v){ for(;x <= n;x += lowbit(x)) tr[x] += v; }
inline int query(int x){ int ret = 0; for(;x;x -= lowbit(x)) ret += tr[x]; return ret; }

void init(){
    for(int i = 1;i <= n;i ++){
        memset(tr,0,sizeof(tr));
        for(int sum = 0,j = i;j <= n;j ++){
            sum += (j - i) - query(a[j]);
            inv[i][j] = sum;
            modify(a[j],1);
        }
    }
}

int f[M][M],id;
void solve(int L,int R,int l,int r){
    if(L>R)return;
    int mid = L+R>>1,p=mid,res = 2e9;
    for(int i=max(mid+1,l);i<=r;i++)
        if(res>f[id-1][i]+inv[mid][i-1])
            res = f[id-1][i]+inv[mid][i-1], p = i;
    f[id][mid] = res;
    // cout<<mid<<' '<<res<<endl;
    solve(L,mid-1,l,p);
    solve(mid+1,R,p,r);
}

signed main(){
    ios::sync_with_stdio(false);
    int TC;
    cin >> TC;
    while(TC --){
        cin >> n >> k;
        int m = 0;
        for(int i = 1;i <= n;i ++){
            cin >> a[i];
            b[++ m] = a[i];
        }
        sort(b + 1,b + 1 + m);
        m = unique(b + 1,b + 1 + m) - b - 1;
        for(int i = 1;i <= n;i ++) a[i] = lower_bound(b + 1,b + 1 + m,a[i]) - b;
        init();
        for(int i=n;i>=1;i--)f[1][i] = inv[i][n];
        for(int i=2;i<=k+1;i++){
            id = i;
            solve(1,n,1,n);
            // for(int j=1;j<=n;j++)
            //     cout<<f[i][j]<<' ';
            // cout<<endl;
        }
        printf("%d\n",f[k+1][1]);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3780kb

input:

2
6 1
4 5 6 2 2 1
6 2
4 5 6 2 2 1

output:

2
0

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 10ms
memory: 4000kb

input:

349
6 2
2 1 2 1 2 1
48 12
42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21
48 12
1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...

output:

1
18
15
145
0
2
1
0
13
13
23
0
0
0
1
0
0
0
0
0
0
0
0
161
3
0
0
1
0
2000000000
0
0
0
0
1
2000000000
3
0
0
1
0
0
1
0
0
1
4
0
0
0
0
0
0
0
0
2
0
2
0
2000000000
8
280
0
0
34
4
0
1
0
0
3
0
0
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
2
0
0
0
0
0
0
0
8
1
8
0
0
0
0
1
11
5
0
0
0
6
0
2000000000
0
0
0
1
0
0...

result:

wrong answer 30th lines differ - expected: '0', found: '2000000000'