QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#544798#7859. BladestormXfJbUhpyzgaW#WA 34ms3960kbC++143.4kb2024-09-02 20:17:442024-09-02 20:17:44

Judging History

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

  • [2024-09-02 20:17:44]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:3960kb
  • [2024-09-02 20:17:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
// 320
const int N=1e5+5;
int s[N],f[N],g[N],fir[N],nxt[N],is[N];
int n,k,B;
void maintain(int id){
    int l=(id-1)*B+1,r=min(id*B,n);
    fir[id]=0;
    for (int i=l;i<=r;++i){
        if (is[i] && !fir[id]) fir[id]=i;
        s[i]=(i==l?0:s[i-1])+is[i];
    }
    for (int i=r,lst=0;i>=l;--i){
        nxt[i]=lst;
        if (is[i]) lst=i;
        if (!nxt[i]){
            f[i]=i;
            g[i]=0;
        }
        else{
            if (i+k<nxt[i]){
                f[i]=f[nxt[i]];
                g[i]=g[nxt[i]]+1;
            }
            else{
                if (i+k>r){
                    f[i]=i;
                    g[i]=0;
                }
                else{
                    f[i]=f[i+k];
                    g[i]=g[i+k]+1;
                }
            }
        }
    }
}
int findnxt(int x){
    for (int i=(x+B-1)/B+1;i<=(n+B-1)/B;++i)
        if (fir[i]) return fir[i];
    return -1;
}
int sum(int l,int r){
    int itl=(l+B-1)/B,itr=(r+B-1)/B;
    if (itl==itr) return s[r]-s[l]+is[l];
    int ans=s[itl*B]-s[l]+is[l]+s[r];
    for (int i=itl+1;i<itr;++i) ans+=s[i*B];
    return ans;
}
int solve(){
    if (k>=B){
        int pos=0,ans=0;
        if (sum(1,k)){
            pos=k;
            ans=1;
        }
        else{
            pos=findnxt(1);
            ans=1;
        }
        while (pos<n){
            if (pos+k>n){
                if (sum(pos+1,n)) ans+=1;
                break;
            }
            else{
                if (sum(pos+1,pos+k)){
                    pos+=k;
                    ans+=1;
                }
                else{
                    pos=findnxt(pos);
                    if (pos==-1) break;
                    ans+=1;
                }
            }
        }
        return ans;
    }
    else{
        int pos=0,ans=0;
        if (s[k]) pos=k,ans=1;
        else{
            for (int i=1;i<=(n+B-1)/B;++i)
                if (fir[i]){
                    pos=i,ans=1;
                    break;
                }
        }
        while (pos<n){
            ans+=g[pos];
            pos=f[pos];
            int id=(pos+B-1)/B;
            if (id==(n+B-1)/B){
                if (s[n]-s[pos]) ans+=1;
                break;
            }
            else if (pos+k>=n){
                if (sum(pos+1,n)) ans+=1;
                break;
            }
            else{
                if (sum(pos+1,pos+k)){
                    pos+=k;
                    ans+=1;
                }
                else{
                    bool flag=0;
                    for (int i=id+1;i<=(n+B-1)/B;++i)
                        if (fir[i]){
                            pos=fir[i];
                            ans++;
                            flag=1;
                            break;
                        }
                    if (!flag) break;
                }
            }
        }
        return ans;
    }
}
void work(){
    scanf("%d%d",&n,&k);B=sqrt(n);
    for (int i=1;i<=n;++i) is[i]=0;
    for (int i=1;i<=(n+B-1)/B;++i) maintain(i);
    for (int x,i=1;i<=n;++i){
        scanf("%d",&x);
        is[x]=1;maintain((x+B-1)/B);
        printf("%d%c",solve()," \n"[i==n]);
    }
}
int main(){
    // freopen("c.in","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3960kb

input:

3
7 2
4 7 3 6 1 2 5
11 3
10 7 4 9 6 8 5 1 3 2 11
9 2
1 2 3 7 8 9 4 5 6

output:

1 2 3 3 4 4 4
1 2 3 3 3 3 3 4 4 4 4
1 1 2 3 4 4 4 5 5

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 34ms
memory: 3816kb

input:

40319
1 1
1
2 1
1 2
2 1
2 1
2 2
1 2
2 2
2 1
3 1
1 2 3
3 1
1 3 2
3 1
2 1 3
3 1
2 3 1
3 1
3 1 2
3 1
3 2 1
3 2
1 2 3
3 2
1 3 2
3 2
2 1 3
3 2
2 3 1
3 2
3 1 2
3 2
3 2 1
3 3
1 2 3
3 3
1 3 2
3 3
2 1 3
3 3
2 3 1
3 3
3 1 2
3 3
3 2 1
4 1
1 2 3 4
4 1
1 2 4 3
4 1
1 3 2 4
4 1
1 3 4 2
4 1
1 4 2 3
4 1
1 4 3 2
4 1
...

output:

1
1 2
1 2
1 1
1 1
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 1 2
1 2 2
1 1 2
1 2 2
1 2 2
1 2 2
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
2 2 3 4
2 2 3 4
2 3 3 4
2 3 4 4
2 3 3 4
2 3 4 4
2 2 3 4
2 2 3 4
2 3 3 4
2 3 4 4
2 3 3 4
2 3 4 4
2 2 3 4
2 2 3 4
2 3 3 4
2 3 4 ...

result:

wrong answer 30th lines differ - expected: '1 2 3 4', found: '2 2 3 4'