QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#572070#9314. The Median of the Median of the MedianwsirAC ✓985ms66192kbC++236.5kb2024-09-18 11:41:232024-09-18 18:14:04

Judging History

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

  • [2024-09-18 18:14:04]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:985ms
  • 内存:66192kb
  • [2024-09-18 18:08:17]
  • hack成功,自动添加数据
  • (/hack/838)
  • [2024-09-18 11:41:23]
  • 评测
  • 测评结果:100
  • 用时:952ms
  • 内存:66316kb
  • [2024-09-18 11:41:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define mii map<int, int>
#define vll vector<ll>
#define vi vector<int>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define lowbit(x) (x & (-x))
#define endl '\n'
const ll N = 1e5;
const ll M = 500 + 10;
const ll mod = 998244353;
const ll inf = 1e17;
const double eps = 1e-6;
int dx[10] = {1, 1, -1, -1, -1, -1, 1, 1, 0}, dy[10] = {1, -1, 1, -1, 1, -1, 1, -1, 0}; // 0左 1右 2下 3上
struct Segment
{
    Segment(ll n)
    {
        seg.resize((n + 10) << 2);
        cn1.resize(n + 10);
    }

    struct info
    {
        ll sum;
        info() {}
        info(ll a) : sum(a) {}
    };

    struct tag
    {
        ll t1;
    };

    struct sb
    {
        info val;
        tag t;
        ll sz;
    };

    info info_merge(info l, info r) // 信息结合函数
    {
        info a;
        a.sum = l.sum + r.sum;
        return a;
    }

    info i_tmerge(info v, tag t) // 标记作用函数
    {
        info a;
        a = v;
        a.sum += t.t1;
        return a;
    }

    tag tag_merge(tag l, tag r) // 标记结合函数
    {
        tag t;
        t.t1 = l.t1 + r.t1;
        return t;
    }

    vector<sb> seg;
    vector<ll> cn1;

    void update(ll id) // 向上更新
    {
        seg[id].val = info_merge(seg[id * 2].val, seg[id * 2 + 1].val);
    }

    void settag(ll id, tag t) // 设置懒标记
    {
        seg[id].t = tag_merge(seg[id].t, t);
        t.t1 *= seg[id].sz;
        seg[id].val = i_tmerge(seg[id].val, t);
    }

    void pushdown(ll id) // 下传懒标记
    {
        if (!seg[id].t.t1)
        {
            return;
        }
        settag(id * 2, seg[id].t);
        settag(id * 2 + 1, seg[id].t);
        seg[id].t = {0};
    }

    void build(ll id, ll l, ll r) // 建树
    {
        seg[id].sz = r - l + 1;
        if (l == r)
        {
            seg[id].val = info(0);
        }
        else
        {
            ll mid = (l + r) >> 1;
            build(id * 2, l, mid);
            build(id * 2 + 1, mid + 1, r);
            update(id);
        }
    }

    void change(ll id, ll l, ll r, ll pos, tag val) // 单改
    {
        if (l == r)
        {
            seg[id].val = i_tmerge(seg[id].val, val);
        }
        else
        {
            pushdown(id);
            ll mid = (l + r) >> 1;
            if (pos <= mid)
                change(id * 2, l, mid, pos, val);
            else
                change(id * 2 + 1, mid + 1, r, pos, val);
            update(id);
        }
    }

    void modify(ll id, ll l, ll r, ll ql, ll qr, tag t) // 区改
    {
        if (l >= ql && r <= qr)
        {
            settag(id, t);
            return;
        }
        ll mid = (l + r) >> 1;
        pushdown(id);
        if (qr <= mid)
            modify(id * 2, l, mid, ql, qr, t);
        else if (ql > mid)
            modify(id * 2 + 1, mid + 1, r, ql, qr, t);
        else
        {
            modify(id * 2, l, mid, ql, mid, t);
            modify(id * 2 + 1, mid + 1, r, mid + 1, qr, t);
        }
        update(id);
    }

    info query(ll id, ll l, ll r, ll ql, ll qr) // 区查
    {
        // assert(ql > qr);
        if (l >= ql && r <= qr)
        {
            return seg[id].val;
        }
        ll mid = (l + r) >> 1;
        pushdown(id);
        if (qr <= mid)
            return query(id * 2, l, mid, ql, qr);
        else if (ql > mid)
            return query(id * 2 + 1, mid + 1, r, ql, qr);
        else
            return info_merge(query(id * 2, l, mid, ql, mid), query(id * 2 + 1, mid + 1, r, mid + 1, qr));
    }

    info check(ll id, ll l, ll r, ll pos) // 单查
    {
        if (l == r && l == pos)
        {
            return seg[id].val;
        }
        ll mid = (l + r) >> 1;
        pushdown(id);
        if (pos <= mid)
            return check(id * 2, l, mid, pos);
        else
            return check(id * 2 + 1, mid + 1, r, pos);
    }

    ll bs(ll id, ll l, ll r, ll x) // 二分
    {
        if (l == r)
        {
            return l;
        }
        ll mid = (l + r) >> 1;
        if (seg[id * 2].val.sum >= x)
            return bs(id * 2, l, mid, x);
        else
            return bs(id * 2 + 1, mid + 1, r, x - seg[id * 2].val.sum);
    }
};

void solve()
{
    ll n;
    cin >> n;
    vll cn(n + 1);
    vll rk;
    for (int i = 1; i <= n; i++)
    {
        cin >> cn[i];
        rk.push_back(cn[i]);
    }
    sort(rk.begin(), rk.end());
    rk.erase(unique(rk.begin(), rk.end()), rk.end());
    ll m = rk.size() + 1;
    vector<vll> b(n + 1, vll(n + 1));
    Segment tr(rk.size() + 1);
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j++)
        {
            ll s = lower_bound(rk.begin(), rk.end(), cn[j]) - rk.begin() + 1;
            tr.change(1, 1, m, s, {1});
            b[i][j] = rk[tr.bs(1, 1, m, (j - i + 2) / 2) - 1];
        }
        tr.build(1, 1, m);
    }
    ll l = 1, r = 1e9;
    function<bool(ll)> check = [&](ll mid)
    {
        vector<vll> pre(n + 1, vll(n + 1));
        for (int i = 1; i <= n; i++)
        {
            for (int j = i; j <= n; j++)
            {
                if (b[i][j] <= mid)
                {
                    pre[i][j] = 1;
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                pre[i][j] += pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1];
            }
        }
        ll cnt = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i; j <= n; j++)
            {
                ll sum = pre[j][j] - pre[i - 1][j] - pre[j][i - 1] + pre[i - 1][i - 1];
                ll len = j - i + 1;
                if (sum >= (len * (len + 1) / 2 + 1) / 2)
                {
                    cnt++;
                }
            }
        }
        return cnt >= (n * (n + 1) / 2 + 1) / 2;
    };
    while (l < r)
    {
        ll mid = (l + r) >> 1;
        if (check(mid))
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    cout << r << endl;
}
int main()
{
    IOS;
    ll t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++)
    {
        solve();
    }
    return 0;
}
/*
 */

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 3 1 7

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

8
3 3 8 4 5 3 8 5

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 848ms
memory: 59240kb

input:

1883
935804604 209383625 842052635 830082014 365721046 29571412 503828250 261878653 304868479 615753663 149387882 137293208 553441715 659054561 809401479 786598486 257715598 738987349 749751119 675212261 214984147 816730618 204108936 529505526 670681192 375128179 445679706 531625791 954119640 739969...

output:

484006473

result:

ok 1 number(s): "484006473"

Test #4:

score: 0
Accepted
time: 947ms
memory: 63680kb

input:

1957
872909724 707949349 490997221 189511043 696381097 482433184 174043836 548201426 788208141 757294560 536411878 361961 65102705 599648900 120639869 620050728 789988994 932102606 540785122 931710164 286821745 583586084 633444407 849735317 780584308 266550416 98570723 722783990 500921667 819344705 ...

output:

516436748

result:

ok 1 number(s): "516436748"

Test #5:

score: 0
Accepted
time: 2ms
memory: 3736kb

input:

178
692096263 317825236 931010188 434850173 52089022 326215531 68436615 315432237 122955263 641096948 348783853 876201150 853561741 370774198 822929299 828427226 546309487 178339038 549855187 410459983 953700209 566391036 649762057 955693969 976755106 277332611 380022551 211888435 42883285 988396877...

output:

593518959

result:

ok 1 number(s): "593518959"

Test #6:

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

input:

173
69392871 816390960 874922071 794279201 87781777 334366903 738652201 601755009 166103436 192703253 735807848 589526799 219998539 16401241 279391882 366912172 228325988 371454295 781080678 111668287 170762000 333246502 638906040 570891055 791690926 463722145 178137760 257822443 589685312 67772391 ...

output:

408439899

result:

ok 1 number(s): "408439899"

Test #7:

score: 0
Accepted
time: 985ms
memory: 66172kb

input:

2000
844787697 717725741 297174490 620713157 869654860 100305714 252182391 350740797 989692676 884515006 283929742 228386094 243395665 931009658 504278816 417368345 996825244 840349536 30435490 464970260 925567493 320798785 588704893 225104969 963755486 262212671 155822732 272322421 979407661 217426...

output:

493853563

result:

ok 1 number(s): "493853563"

Test #8:

score: 0
Accepted
time: 978ms
memory: 66192kb

input:

2000
125514461 95022349 795740214 269657744 524051185 725933061 555301058 20956383 570982744 927663180 130503343 910377386 251688610 442670648 444873155 283896336 535310191 667590228 223550748 256004263 626775796 392636383 500784551 509216248 283985276 517339979 342212266 70437630 465533157 91397139...

output:

477878585

result:

ok 1 number(s): "477878585"

Extra Test:

score: 0
Extra Test Passed