QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#817257#9865. Dollsrqoi031WA 6ms3688kbC++202.4kb2024-12-16 21:07:382024-12-16 21:07:39

Judging History

This is the latest submission verdict.

  • [2025-01-08 13:49:40]
  • hack成功,自动添加数据
  • (/hack/1434)
  • [2024-12-16 21:07:39]
  • Judged
  • Verdict: WA
  • Time: 6ms
  • Memory: 3688kb
  • [2024-12-16 21:07:38]
  • Submitted

answer

#include<stdio.h>
#include<algorithm>
#include<numeric>
constexpr int N{100000};
int a[N+5],b[N+5],p[N+5];
struct segment {
    int l,r;
    constexpr segment():l{},r{} {}
    constexpr segment(const int &v):l{v},r{v} {}
    constexpr bool adjacent_to(const segment &x) const {
        return r+1==x.l||x.r+1==l;
    }
    constexpr void include(const segment &x) {
        l=std::min(l,x.l),r=std::max(r,x.r);
    }
};
segment seg[N+5];
int vis[N+5];
int pre[N+5],nxt[N+5];
int que[(N<<1)+5];
void solve() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",a+i);
    }
    const auto check([&](const int &l,const int &r)->bool {
        if(l==r) {
            return true;
        }
        const int len{r-l+1};
        std::copy(a+l,a+r+1,b+1);
        std::sort(b+1,b+len+1);
        for(int i=l;i<=r;i++) {
            seg[i]=p[i]=std::lower_bound(b+1,b+len+1,a[i])-b;
        }
        std::fill(vis+l,vis+r+1,0);
        std::iota(pre+l,pre+r+1,l-1),pre[l-1]=l-1;
        std::iota(nxt+l,nxt+r+1,l+1),nxt[r+1]=r+1;
        int cnt{0};
        int qb{1},qe{0};
        const auto try_insert([&](const int &x)->void {
            if(seg[x].adjacent_to(seg[nxt[x]])) {
                que[++qe]=x;
            }
        });
        for(int i=l;i<r;i++) {
            try_insert(i);
        }
        while(qb<=qe) {
            int &u{que[qb++]};
            if(!vis[u]&&seg[u].adjacent_to(seg[nxt[u]])) {
                ++cnt;
                seg[u].include(seg[nxt[u]]);
                vis[nxt[u]]=1;
                pre[nxt[nxt[u]]]=u;
                nxt[u]=nxt[nxt[u]];
                if(pre[u]>=l) {
                    try_insert(pre[u]);
                }
                if(nxt[u]<=r) {
                    try_insert(u);
                }
            }
        }
        return cnt==len-1;
    });
    int ans{0};
    for(int i=1;i<=n;) {
        int len{1};
        while(i+len-1<n&&check(i,std::min(i+(len<<1)-1,n))) {
            len<<=1;
        }
        int l{i+len},r{std::min(i+(len<<1)-1,n)};
        while(l<=r) {
            int mid{l+r>>1};
            if(check(i,mid)) {
                l=mid+1;
            }
            else {
                r=mid-1;
            }
        }
        ++ans,i=l;
    }
    printf("%d\n",n-ans);
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

8
4
2 1 4 3
4
1 4 2 3
4
3 1 4 2
5
1 3 5 2 4
5
1 4 2 5 3
5
2 5 3 1 4
6
1 3 6 5 2 4
6
2 5 1 3 6 4

output:

3
3
2
3
3
3
4
4

result:

ok 8 numbers

Test #2:

score: -100
Wrong Answer
time: 6ms
memory: 3644kb

input:

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

output:

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

result:

wrong answer 10th numbers differ - expected: '3', found: '2'