QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#703782#7904. Rainbow Subarraywyh123WA 116ms5988kbC++203.2kb2024-11-02 18:29:562024-11-02 18:29:57

Judging History

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

  • [2024-11-02 18:29:57]
  • 评测
  • 测评结果:WA
  • 用时:116ms
  • 内存:5988kb
  • [2024-11-02 18:29:56]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

const int N = 1e6 + 10;
int a[N];

priority_queue<int, vector<int>, greater<int>> maxn, fmaxn;
priority_queue<int> minn, fminn;

void del_greater( int x , priority_queue< int , vector<int> , greater<int> >& teg, priority_queue< int , vector<int> , greater<int> >& del_teg )
{
    del_teg.push( x );
    while( !del_teg.empty() && del_teg.top()==teg.top() ){
        del_teg.pop();
        teg.pop();
    }
}

void del_less( int x , priority_queue< int , vector<int> , less<int> >& teg , priority_queue< int , vector<int> , less<int> >& del_teg )
{
    del_teg.push( x );
    while( !del_teg.empty() && del_teg.top()==teg.top() ){
        del_teg.pop();
        teg.pop();
    }
}

void solve () {
    int n, k, sz = 0, xz = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        a[i] -= i;
    }

    int ans = 0, shang = 0, xia = 0, l = 1;
    for (int r = 1; r <= n; r ++) {
        maxn.push(a[r]);
        sz ++;
        shang += a[r];
        while (sz > xz + 1) {
            int p = maxn.top(); maxn.pop();
            shang -= p; sz --;

            minn.push(p);
            xia += p; xz ++;
        }

        while (xz > sz) {
            int p = minn.top(); minn.pop();
            xz --; xia -= p;
            maxn.push(p);
            sz ++; shang += p;
        }

        while (xz > 0 && maxn.top() < minn.top()) {
            int p = maxn.top();
            int s = minn.top();
            maxn.pop(), minn.pop();
            shang -= p, xia -= s;
            maxn.push(s), minn.push(p);
            shang += s, xia += p;
        }

        while ((shang - maxn.top() * sz) + (maxn.top() * xz - xia) > k) {
            if (a[l] >= maxn.top()) {
                del_greater(a[l], maxn, fmaxn);
                sz --; shang -= a[l];
            } else {
                del_less(a[l], minn, fminn);
                xz --; xia -= a[l];
            }

            while (xz > sz) {
                int p = minn.top(); minn.pop();
                xz --; xia -= p;
                maxn.push(p);
                sz ++; shang += p;
            }

            while (sz > xz + 1) {
                int p = maxn.top(); maxn.pop();
                sz --; shang -= p;
                minn.push(p);
                xz ++; xia += p;
            }

            while (xz > 0 && maxn.top() < minn.top()) {
                int p = maxn.top();
                int s = minn.top();
                maxn.pop(), minn.pop();
                shang -= p, xia -= s;
                maxn.push(s), minn.push(p);
                shang += s, xia += p;
            }
            l ++;
        }

        ans = max(ans, r - l + 1);
    }

    cout << ans << endl;

    while (maxn.size()) maxn.pop();
    while (minn.size()) minn.pop();
    while (fmaxn.size()) fmaxn.pop();
    while (fminn.size()) fminn.pop();
}

signed main () {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;

    cin >> t;
    while (t --) {
        solve ();
    }
    return 0;
}
/*
5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3
*/

詳細信息

Test #1:

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

input:

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3

output:

4
3
5
1
1

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 116ms
memory: 5988kb

input:

11102
2 167959139
336470888 134074578
5 642802746
273386884 79721198 396628655 3722503 471207868
6 202647942
268792718 46761498 443917727 16843338 125908043 191952768
2 717268783
150414369 193319712
6 519096230
356168102 262263554 174936674 407246545 274667941 279198849
9 527268921
421436316 3613460...

output:

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

result:

wrong answer 1358th lines differ - expected: '6', found: '4'