QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300567#4936. Shopping ChangesLeeShoW#WA 149ms13288kbC++202.8kb2024-01-08 14:26:562024-01-08 14:26:56

Judging History

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

  • [2024-01-08 14:26:56]
  • 评测
  • 测评结果:WA
  • 用时:149ms
  • 内存:13288kb
  • [2024-01-08 14:26:56]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h> 
#define int long long
using namespace std;
struct BIT{
    int n;
    vector<int>bit;
    void init(int _n){
        n=_n+1;
        bit=vector<int>(n,0);
    }
    int lowbit(int x){
        return x&-x;
    }
    int query(int x){
        int res=0;
        x++;
        for(;x>0;x-=lowbit(x)){
            res+=bit[x];
        }
        return res;
    }
    void upd(int x,int v){
        x++;
        for(;x<n;x+=lowbit(x)){
            bit[x]+=v;
        }
    }
}a,b,c;
int all[100005];
vector<int>player[100005];
int n,m;
int sol1(){
    int res=0;
    a.init(200005);
    for(int i=n-1;i>=0;i--){
        res+=a.query(all[i]-1);
        a.upd(all[i],1);
    }
    return res;
}
int sol2(int id){
    int res=0;
    int n=player[id].size();
    for(int i=n-1;i>=0;i--){
        //cout<<"Asdas";
        res+=b.query(player[id][i]-1);
        b.upd(player[id][i],1);
    }
    for(int i=0;i<n;i++){
        b.upd(player[id][i],-1);
    }
    return res;
}
signed main() {
    cin>>n>>m;
    vector<int> inq;
    for(int i=0;i<n;i++){
        cin>>all[i];
        inq.push_back(all[i]);
    }
    for(int i=0;i<m;i++){
        int nn;
        cin>>nn;
        while(nn--){
            int k;
            cin>>k;
            player[i].push_back(k);
            inq.push_back(k);
        }
    }
    sort(inq.begin(),inq.end());
    inq.erase(unique(inq.begin(),inq.end()),inq.end());
    for(int i=0;i<n;i++){
        all[i]=lower_bound(inq.begin(),inq.end(),all[i])-inq.begin();
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<player[i].size();j++){
            player[i][j]=lower_bound(inq.begin(),inq.end(),player[i][j])-inq.begin();
        }
    }
    int ans=sol1();//算第一個逆敘述對
    b.init(200005);
    for(int i=0;i<m;i++){
        int now_ans=ans+sol2(i);
        vector<int>suf,pre;
        for(int j=0;j<player[i].size();j++){
            int now=player[i][j];
            pre.push_back(a.query(now-1));
        }
        for(int j=0;j<player[i].size();j++){
            int now=player[i][j];
            suf.push_back(n-a.query(now));
        }
        for(int i=1;i<pre.size();i++){
            pre[i]+=pre[i-1];
        }
        for(int i=suf.size()-2;i>=0;i--){
            suf[i]+=suf[i+1];
        }
        /*cout<<i<<":"<<endl;
        for(auto ii:pre){
            cout<<ii<<" ";
        }cout<<endl;
        for(auto ii:suf){
            cout<<ii<<" ";
        }cout<<endl;*/
        
        int add=1e9;
        add=min(add,min(pre.back(),suf[0]));
        for(int ii=0;ii<pre.size()-1;ii++){
            add=min(add,pre[ii]+suf[ii+1]);
        }
        //cout<<now_ans<<" "<<add<<endl;
        cout<<now_ans+add<<endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8632kb

input:

3 3
5 6 7
6 2 3 4 8 9 10
2 100 99
3 5 6 7

output:

0
1
1

result:

ok 3 lines

Test #2:

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

input:

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

output:

3
27

result:

ok 2 lines

Test #3:

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

input:

1 1
589284012
1 767928734

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 91ms
memory: 12828kb

input:

10000 9999
298772847 712804102 869795012 527998188 804246450 598105371 843966934 639805471 937482040 887551242 254734680 188704975 17408126 626523673 553956319 697723205 231690822 637079761 232393146 377026277 962223856 338922458 912529500 710873344 942955137 51167037 195729799 529674367 990599310 4...

output:

24802338
24830913
24857654
24813132
24846150
24785558
24857315
24805175
24862714
24785339
24804999
24780907
24808009
24798987
24958122
24781372
24772291
24846071
24953540
24778276
24778689
24979527
24770012
24892097
24776909
24865295
24821506
24772586
24800432
24891424
24864488
24820312
24801522
248...

result:

ok 9999 lines

Test #5:

score: -100
Wrong Answer
time: 149ms
memory: 13288kb

input:

42320 25000
977178721 305456426 916831455 324594376 259922325 798438534 906876242 353428436 459214642 504133134 734517252 944888626 929971853 735313273 285979369 866298401 385768124 918185862 811827492 3054135 190006456 852394509 784943097 903969029 4089198 931644108 916374905 942243264 383987411 45...

output:

7099368115874478341
7099368115874465587
311430788976440113
-1841482351968039938
7099368115874374528
5733194940788188842
7099368115874361505
7099368115874421733
7099368117211064597
2491039315218897595
1899488842661093993
7099368115874370694
-439488994926199723
7081350307313671987
4947459519161514326
...

result:

wrong answer 1st lines differ - expected: '448869835', found: '7099368115874478341'