QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#189379#7504. HearthStoneucup-team1209#AC ✓906ms77120kbC++203.4kb2023-09-27 13:49:422023-09-27 13:49:43

Judging History

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

  • [2023-09-27 13:49:43]
  • 评测
  • 测评结果:AC
  • 用时:906ms
  • 内存:77120kb
  • [2023-09-27 13:49:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
template<typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template<typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
    #ifdef zqj
    freopen("a.in","r",stdin);
    #endif
}
typedef long long ll;
#define sz 2010100

int n;
int cnt[sz];

ll ww[sz];

#define ls k<<1
#define rs k<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
int tr1[sz<<2]; ll tr2[sz<<2];
void modify(int k,int l,int r,int x,int w) {
    tr1[k]+=w,tr2[k]+=w*ww[x];
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) modify(lson,x,w);
    else modify(rson,x,w);
}
pair<int,ll> query(int k,int l,int r,int x) {
    if (tr1[k]<=x) return {tr1[k],tr2[k]};
    if (l==r) return {x,x*ww[l]};
    int mid=(l+r)>>1;
    auto L=query(lson,x);
    if (L.fir==x) return L;
    auto R=query(rson,x-L.fir);
    return {L.fir+R.fir,L.sec+R.sec};
}
void debug(int k,int l,int r) {
    if (l==r) {
        rep(_,1,tr1[k]) cerr<<ww[l]<<' ';
        return;
    }
    int mid=(l+r)>>1;
    debug(lson),debug(rson);
}

int main() {
    file();
    ios::sync_with_stdio(false),cin.tie(0);
    cin>>n;
    ll sum=0;
    rep(i,1,n) {
        int x; cin>>x;
        if (x>n) sum+=x-n,x=n;
        ++cnt[x];
    }
    const ll inf=2e12;
    priority_queue<ll,vector<ll>,greater<ll>>q1,q2;
    rep(i,1,n) {
        if (q1.size()) {
            ll t=q1.top(); q1.pop();
            ww[i]=-t-i-i;
            q2.push(ww[i]);
        }
        else q2.push(-i-inf),ww[i]=-i-inf;
        rep(_,1,cnt[i]) {
            if (q2.size()&&q2.top()+i<0) {
                ll t=q2.top(); q2.pop();
                q1.push({-t-i-i});
            }
            else q1.push({-i});
        }
    }
    rep(i,1,n) ww[i+n]=-i;
    sort(ww+1,ww+2*n+1);
    int res1=n; ll res2=0;
    rep(i,1,n) res2+=1ll*i*cnt[i];
    while (q1.size()) q1.pop();
    while (q2.size()) q2.pop();
    auto id=[](ll w) {
        return lower_bound(ww+1,ww+2*n+1,w)-ww;
    };
    ll ans=1e18;
    rep(i,1,n) {
        if (q1.size()) {
            ll t=q1.top(); q1.pop();
            sum+=t+i;
            q2.push(-t-i-i);
            modify(1,1,n*2,id(-t-i-i),1);
        }
        else sum+=inf,q2.push(-i-inf),modify(1,1,n*2,id(-i-inf),1);
        rep(_,1,cnt[i]) {
            if (q2.size()&&q2.top()+i<0) {
                ll t=q2.top(); q2.pop();
                modify(1,1,n*2,id(t),-1);
                sum+=t+i;
                q1.push({-t-i-i});
            }
            else q1.push({-i});
        }
        res1-=cnt[i],res2-=1ll*i*cnt[i];
        // priority_queue<ll,vector<ll>,greater<ll>>q3=q2;
        // ll cur=sum+res2;
        // rep(_,1,res1) {
        //     if (q3.size()&&q3.top()<-i) cur+=q3.top(),q3.pop();
        //     else cur+=-i;
        // }
        // chkmin(ans,cur);
        modify(1,1,n*2,id(-i),n);
        // debug(1,1,2*n);
        // cerr<<'\n';
        ll cur=sum+res2;
        auto www=query(1,1,2*n,res1);
        cur+=www.sec;
        modify(1,1,n*2,id(-i),-n);
        chkmin(ans,cur);

        // debug(1,1,2*n);
        // cerr<<'\n';
        // auto q3=q2;
        // while (q3.size()) cerr<<q3.top()<<' ',q3.pop();
        // cerr<<'\n';
    }
    cout<<ans<<'\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9612kb

input:

6
4 6 8 9 2 4

output:

12

result:

ok 1 number(s): "12"

Test #2:

score: 0
Accepted
time: 1ms
memory: 9668kb

input:

15
9 4 9 2 6 6 1 3 10 4 10 2 4 8 6

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

20
5 5 7 5 4 4 10 1 8 1 3 7 7 8 5 2 5 9 5 9

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 1ms
memory: 9544kb

input:

25
2 6 5 12 2 5 5 11 3 5 1 3 7 10 5 4 12 10 4 8 4 12 5 2 8

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 1ms
memory: 9624kb

input:

25
2 2 18 4 17 13 14 1 6 16 20 24 3 3 10 3 10 6 14 12 9 16 19 15 6

output:

9

result:

ok 1 number(s): "9"

Test #6:

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

input:

13
25 5 20 7 15 7 8 3 16 23 10 8 10

output:

66

result:

ok 1 number(s): "66"

Test #7:

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

input:

105
76 60 6 84 64 25 9 10 58 22 46 31 80 16 14 10 75 57 80 60 28 58 43 83 9 44 62 73 76 88 45 21 47 56 52 2 59 69 39 76 19 8 13 17 68 6 49 88 62 53 23 53 14 65 28 13 84 30 64 50 77 57 82 31 11 77 77 72 52 89 86 81 21 57 24 57 35 61 78 24 53 80 82 66 46 51 47 25 24 83 11 17 60 29 21 25 54 76 55 61 19...

output:

129

result:

ok 1 number(s): "129"

Test #8:

score: 0
Accepted
time: 1ms
memory: 9696kb

input:

500
17 5 59 21 20 16 59 8 41 79 50 72 51 45 20 45 46 86 76 28 14 22 11 82 8 23 37 16 75 29 71 52 50 89 54 8 33 65 27 31 47 59 39 13 43 6 55 68 41 78 28 56 44 55 45 46 48 54 50 5 29 52 87 87 71 33 64 6 2 35 27 39 72 48 20 69 48 23 22 87 31 26 44 80 5 22 3 82 67 39 29 58 56 81 68 12 49 30 49 68 86 69 ...

output:

1

result:

ok 1 number(s): "1"

Test #9:

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

input:

1000
9 81 55 45 54 13 14 32 88 21 39 13 100 20 14 81 72 30 53 66 73 37 59 43 3 1 20 60 60 15 14 45 13 77 17 34 9 4 21 39 79 43 84 89 35 71 55 93 72 20 66 11 60 64 94 65 71 5 17 49 84 11 42 24 6 66 3 89 22 53 97 14 66 36 59 29 54 99 81 25 92 35 18 74 93 13 80 30 18 89 31 38 32 65 97 41 52 77 48 95 97...

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 0
Accepted
time: 5ms
memory: 10320kb

input:

10000
184 50 415 13 913 24 597 640 706 688 37 927 774 169 714 941 894 926 323 545 585 650 52 839 391 809 596 153 522 119 199 170 247 492 93 541 323 792 835 753 673 579 344 256 171 24 698 480 63 11 570 401 524 297 725 896 84 447 412 147 885 126 439 38 355 493 526 654 664 168 440 276 583 503 799 381 3...

output:

0

result:

ok 1 number(s): "0"

Test #11:

score: 0
Accepted
time: 8ms
memory: 13944kb

input:

10000
4210 3733 4488 912 894 670 496 1222 727 3271 2853 4279 3277 1468 643 2574 2062 256 3969 135 605 4426 99 187 1143 4975 4225 4887 4162 4360 207 424 391 1637 3094 946 1001 3937 3699 2433 1038 4968 2374 527 3086 2967 2366 3887 2618 4964 1844 1089 2750 1469 1685 3808 1010 2205 3388 2328 2797 3852 2...

output:

846

result:

ok 1 number(s): "846"

Test #12:

score: 0
Accepted
time: 3ms
memory: 9776kb

input:

3000
9265 2616 8267 9004 4633 1888 7872 6994 1512 9826 339 5028 9190 612 2462 7409 9216 5805 7323 1838 8061 458 5975 3364 17 9134 6022 5061 5202 458 5687 9301 2842 2877 9593 6199 3181 2142 8238 2053 6940 680 2303 516 760 7237 1633 9303 1402 5438 8774 1170 8819 6066 3254 9378 255 8053 768 5740 5056 1...

output:

9656961

result:

ok 1 number(s): "9656961"

Test #13:

score: 0
Accepted
time: 4ms
memory: 9820kb

input:

4000
4295 6048 205 13966 1929 4461 13116 8417 14044 7261 4008 5424 6832 5689 10584 4612 6202 7792 1032 4184 12497 3714 37 10959 5372 359 6191 7270 6835 5083 9928 12924 2150 9973 5324 4279 6683 14201 1681 4992 2244 4234 12434 11466 6519 28 12998 13078 5864 11961 6198 3099 13428 490 6863 7501 10575 33...

output:

19583941

result:

ok 1 number(s): "19583941"

Test #14:

score: 0
Accepted
time: 892ms
memory: 76136kb

input:

1000000
80608 251073 980681 649495 561970 950614 76033 666468 116836 955978 87641 648542 279156 823327 256029 100512 154328 155177 452553 278682 409719 297297 280656 205454 605933 880725 153112 685012 442187 191814 152827 917527 560021 866825 332296 802518 781571 91031 177856 476452 98625 577021 352...

output:

174249444

result:

ok 1 number(s): "174249444"

Test #15:

score: 0
Accepted
time: 887ms
memory: 77120kb

input:

1000000
734923 276989 401041 865275 658138 885662 781455 862400 20539 147942 300911 468741 862949 278149 467363 84100 898146 814366 596848 826284 101267 758474 262831 450206 415081 565142 663885 250864 620205 598464 73611 135086 225914 630078 713599 804308 689469 866551 934034 851989 328700 534727 5...

output:

120543133

result:

ok 1 number(s): "120543133"

Test #16:

score: 0
Accepted
time: 881ms
memory: 74668kb

input:

1000000
171132 261498 584300 815166 377994 229007 359640 132393 892128 103801 807891 26464 435184 742580 511271 675591 383103 264749 70069 266488 412005 291334 734574 397566 29650 655318 897819 703256 824964 116839 462038 86141 475858 160758 451407 977618 864420 286582 276936 952053 40824 634958 758...

output:

128332022

result:

ok 1 number(s): "128332022"

Test #17:

score: 0
Accepted
time: 906ms
memory: 76000kb

input:

1000000
934499 904438 43275 2035 791405 314435 532360 791295 844656 26554 726881 799662 766547 692597 193615 49853 578112 124662 889997 835021 181222 345758 213789 629157 274232 700406 628932 438575 585964 451242 310857 49655 816956 653001 961787 635439 251496 577990 739752 578030 77042 455866 32093...

output:

170812168

result:

ok 1 number(s): "170812168"

Test #18:

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

input:

1000
8 5 16 1 17 20 12 2 5 3 20 11 11 19 8 3 19 18 11 1 9 20 8 3 7 4 16 8 11 7 18 15 2 20 18 14 2 3 19 8 5 17 1 3 4 17 4 10 17 19 11 12 6 20 9 12 3 11 8 3 20 3 5 17 1 2 5 20 19 7 7 4 2 1 3 13 2 15 12 16 12 10 7 2 11 9 19 19 12 8 11 12 17 17 3 18 8 6 7 1 13 2 2 18 3 4 16 5 3 2 17 16 19 19 8 6 5 13 17...

output:

0

result:

ok 1 number(s): "0"