QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300587#4936. Shopping ChangesLeeShoW#WA 146ms13408kbC++202.8kb2024-01-08 14:42:012024-01-08 14:42:02

Judging History

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

  • [2024-01-08 14:42:02]
  • 评测
  • 测评结果:WA
  • 用时:146ms
  • 内存:13408kb
  • [2024-01-08 14:42:01]
  • 提交

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 j=1;j<pre.size();j++){
            pre[j]+=pre[j-1];
        }
        for(int j=suf.size()-2;j>=0;j--){
            suf[j]+=suf[j+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<<endl;
        //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: 0ms
memory: 8584kb

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: 8564kb

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: 9128kb

input:

1 1
589284012
1 767928734

output:

0

result:

ok single line: '0'

Test #4:

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

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: 146ms
memory: 13408kb

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:

3916314418675128357
3916314418675115603
-2871622908222909871
-5024536049167389922
3916314418675024544
2550141243588838858
3916314418675011521
3916314418675071749
3916314420011714613
-692014381980452389
-1283564854538255991
3916314418675020710
-3622542692125549707
3898296610114322003
1764405821962164...

result:

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