QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125905#6627. Line Townstarrylasky0 6ms28260kbC++144.3kb2023-07-17 21:38:242023-07-17 21:38:25

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 21:38:25]
  • 评测
  • 测评结果:0
  • 用时:6ms
  • 内存:28260kb
  • [2023-07-17 21:38:24]
  • 提交

answer

///yl/hs/bx/yl
#include<bits/stdc++.h>
#define fep(i,l,r) for(int i=l;i<=r;++i)
#define feb(i,r,l) for(int i=r;i>=l;--i)
#define For(i,u) for(int i=head[u];i;i=e[i].nxt)
#define LL long long
// #define int long long
#define ld long double
#define pr pair<int,int>
#define mpr make_pair
using namespace std;

const int N = 5e5+5,mod = 1e9+7;
const LL INF = 1e15;
inline int read()
{
   int s=0,w=1; char ch=getchar();
   while(!(ch>='0'&&ch<='9')) {if(ch=='-') w=-1; ch=getchar();}
   while(  ch>='0'&&ch<='9')  {s=(s<<1)+(s<<3)+ch-'0'; ch=getchar();}
   return s*w;
}
inline int Mod(int x) {return x>=mod?x-mod:x;}
inline void addmod(int &x,int y) {x=Mod(x+y);}
inline void chkmin(LL &x,LL y) {x=min(x,y);}

namespace starrylasky
{
    int n,num,S,a[N],b[N],rk[N],id[N],col[N]; vector<int > v[N];
    LL f[2],g[2];
    struct BIT
    {
        #define lowbit(x) (x&-x)
        int sum[N];
        inline void update(int x,int res) {for(;x<=n;x+=lowbit(x)) sum[x]+=res;}
        inline int query(int x)
        {
            int ans=0;
            for(;x;x-=lowbit(x)) ans+=sum[x];
            return ans;
        }
    }b1,b2;

    inline LL calc(int m)
    {
        LL ans=0;// cerr<<m<<"\n";
        feb(i,m,1) ans+=b2.query(rk[id[i]]),b2.update(rk[id[i]],1);
        fep(i,1,m) b2.update(rk[id[i]],-1);
        return ans;
    }

    inline void work(int x,int cl,int cr)
    {
        if(g[cl]==INF) return ;
        int siz=v[x].size(),cnt0=0,cnt1=0,c0=(siz+1)/2,c1=siz/2,n0=0,n1=0;
        vector<int > a1,a0; a1.resize(siz); a0.resize(siz);
        fep(i,1,siz)
        {
            rk[i]=b1.query(v[x][i-1]);
            if(col[v[x][i-1]]) a1[++n1]=i,++cnt1;
            else a0[++n0]=i,++cnt0;
        }
        if(cr) swap(c0,c1);
        if(cl==(cr^((siz-1)&1)))
        {
            if(cnt0!=c0||cnt1!=c1) return ;
		    for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
            LL s=calc(siz);
            fep(i,1,siz) s+=num-(siz-i)-rk[i];
            chkmin(f[cl],g[cl]+s);// cerr<<x<<" "<<s<<" ";
            fep(i,1,siz)
            {
                int j=id[i];
                s+=rk[j]-j-(num-(siz-j)-rk[j]);// cerr<<s<<" ";
                chkmin(f[cl^(i&1)],g[cl]+s);
            }
            //cerr<<"\n";
        }
        else if(c0==cnt0&&c1==cnt1)
        {
		    for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
            LL s=calc(siz);// cerr<<s<<"\n";
            fep(i,1,siz) s+=num-(siz-i)-rk[i];
            chkmin(f[cl],g[cl]+s);// cerr<<"type2:"<<x<<" "<<s<<"\n";
            for(int i=1;i+1<=siz;i+=2)
            {
                int j=id[i],k=id[i+1];
                s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
                chkmin(f[cl],g[cl]+s);// cerr<<j<<" "<<k<<" "<<s<<"\n";
            }
        }
        else
        {
            (cr^((siz-1)&1)?c1:c0)--,(cl?c1:c0)--;
            if(c1!=cnt1||c0!=cnt0) return ;
		    for(int i=1,j=1,k=1;i<=siz;i++)
                if(i==1) id[i]=(cl?a1[j++]:a0[k++]);
                else id[i]=(cr^(siz-i&1)?a1[j++]:a0[k++]);
            LL s=calc(siz);
            fep(i,1,siz) s+=num-(siz-i)-rk[i];
            s+=rk[1]-1-(num-(siz-1)-rk[1]);
            chkmin(f[cl^1],g[cl]+s);
            for(int i=2;i+1<=siz;i+=2)
            {
                int j=id[i],k=id[i+1];
                s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
                chkmin(f[cl^1],g[cl]+s);
            }
        }
    }

    inline void Main()
    {
        n=S=read(); num=n;
        fep(i,1,n) a[i]=read(),b[i]=abs(a[i]);
        sort(b+1,b+1+S); S=unique(b+1,b+1+S)-b-1;
        fep(i,1,n)
        {
            int t=lower_bound(b+1,b+1+S,abs(a[i]))-b;
            v[t].emplace_back(i); a[i]=(a[i]>0?t:-t);
            b1.update(i,1); col[i]=(a[i]<0)^(i&1);
        }
        f[0]=0,f[1]=INF;
        feb(i,S,1)
        {
            fep(j,0,1) g[j]=f[j],f[j]=INF;
            fep(j,0,1) work(i,j,j^(num&1));
            for(auto p:v[i]) b1.update(p,-1);
            num-=v[i].size();
        }
        printf("%d\n",min(f[0],f[1])==INF?-1:min(f[0],f[1]));
    }
}

signed main()
{
    int _T=1;
    while(_T--) starrylasky::Main();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 6ms
memory: 26152kb

input:

10
1 1 1 1 1 -1 -1 -1 1 -1

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 26208kb

input:

10
1 1 1 1 1 1 -1 1 1 -1

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -3
Wrong Answer
time: 1ms
memory: 24084kb

input:

1
-1

output:

-1

result:

wrong answer 1st numbers differ - expected: '0', found: '-1'

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
Wrong Answer

Test #60:

score: 4
Accepted
time: 6ms
memory: 28260kb

input:

10
3 10 5 -9 7 2 -6 1 8 0

output:

-1

result:

ok 1 number(s): "-1"

Test #61:

score: -4
Wrong Answer
time: 1ms
memory: 26168kb

input:

10
-9 0 -1 7 5 10 6 3 2 -8

output:

23

result:

wrong answer 1st numbers differ - expected: '13', found: '23'

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%