wawawawawaw求助

P1908 逆序对

zhangzhiqiao0130 @ 2022-12-18 11:02:10

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<cstdlib>
#include<locale>
#include<stack>
#include<bits/stdc++.h>
using namespace std;
int a[1000001],b[10000001],n,cnt=0;
void merge(int left,int right) {
    int i,j,k,mid=(left+right)/2;
    if(left==right) return ;
    merge(left,mid);
    merge(mid+1,right);
    i=left;
    j=mid+1;
    k=left;
    while(i<=mid && j<=right) {
        if(a[i]>a[j]) {
            b[k++]=a[j++];
        } else {
            b[k++]=a[i++];
            cnt+=mid-i+1;
        }
    }
    while(i<=mid) {
        b[k++]=a[i++];
    }
    while(j<=right) {
        b[k++]=a[j++];
    }
    for(int i=left;i<=right;i++)
    {
        a[i]=b[i];
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    merge(1,n);
    cout<<cnt;
    return 0;
}

by kjhhjki @ 2022-12-18 11:45:17

你这是样例都没过啊

问题1:cnt统计是右半放上来时统计一次,换句话说,

cnt+=mid-i+1;

从29行挪到27行去

问题2:本题似乎会爆int,建议开long long

改后已ac

AC记录


|