QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#134015#4936. Shopping Changeslearner__WA 91ms4416kbC++202.4kb2023-08-02 20:32:102023-08-02 20:32:12

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 20:32:12]
  • 评测
  • 测评结果:WA
  • 用时:91ms
  • 内存:4416kb
  • [2023-08-02 20:32:10]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
using namespace std;

struct BIT {
    int n; vector<int> c;
    BIT(int sz) : n(sz), c(sz + 1) {};
    int lowbit(int x) { return x & -x; }
    void add(int x, int val) {
        while (x <= n) c[x] += val, x += lowbit(x);
    }
    int getsum(int x){
        int ans = 0;
        while (x) ans += c[x], x -= lowbit(x);
        return ans;
    }
    int query(int l, int r){ return getsum(r) - getsum(l - 1); }
    int kth(int k) {//大于等于k的最小位置
    	int res = 0, x = 0; 
    	for (int i = log2(n); i >= 0; --i) {
        	x += 1 << i;                 
        	if (x >= n || res + c[x] >= k) x -= 1 << i;
        	else res += c[x];
    	}
   		return x + 1;
	}
};

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];

    auto v = a;
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    BIT bit(n + 1);
    int t = 0;
    for(int i = 0; i < n; i++)
    {
        int idx = lower_bound(all(v), a[i]) - v.begin() + 1;
        t += i - bit.getsum(idx);
        bit.add(idx, 1);
    }

    sort(all(a));
    while(m--)
    {
        int k;
        cin >> k;
        vector<int> b(k), cnt1(k), cnt2(k);
        int res = 0;
        for(int i = 0; i < k; i++)
        {
            cin >> b[i];
            cnt1[i] = a.end() - upper_bound(all(a), b[i]);
            cnt2[i] = lower_bound(all(a), b[i]) - a.begin();
            res += cnt1[i];

            // if(k == 3) cerr << i << ' ' << cnt1[i] << ' ' << cnt2[i] << '\n';
        }

        auto v = b;
        sort(all(v));
        v.erase(unique(all(v)), v.end());
        BIT bit(k + 1);
        for(int i = 0; i < k; i++)
        {
            int idx = lower_bound(all(v), b[i]) - v.begin() + 1;
            res += i - bit.getsum(idx);
            bit.add(idx, 1);
        }

        int ans = res;
        // if(k == 3) cerr << "res " << res << '\n';
        for(int i = 0; i < k; i++)
        {
            res -= cnt1[i];
            res += cnt2[i];
            ans = min(ans, res);
            // if(k == 3) cerr << res << '\n';
        }

        cout << ans + t << '\n';
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t--) solve();
    return 0;
}

詳細信息

Test #1:

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

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

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: 1ms
memory: 3460kb

input:

1 1
589284012
1 767928734

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 49ms
memory: 3636kb

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: 0
Accepted
time: 70ms
memory: 3640kb

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:

448869835
448857081
449001851
448984953
448766022
449088370
448752999
448813227
448791232
448952235
448991581
448762188
449564834
448895404
448773616
448827147
448822397
448871140
448785180
448915022
448786815
448758118
448930420
449164516
448765912
448826222
448791834
448895668
448789204
448791197
...

result:

ok 25000 lines

Test #6:

score: 0
Accepted
time: 74ms
memory: 3960kb

input:

50000 50000
478377125 98598664 834663974 414981508 481428682 921148788 327891419 311824685 274571883 326908479 516913707 230013528 47214051 844074075 870004539 261165376 338829696 476144552 905505033 857420851 4709519 198366271 90438077 24208873 865090108 250612383 910141082 205796257 712687049 6996...

output:

626276046
626281383
626305127
626284808
626194742
626223647
626345428
626232541
626201578
626246219
626205408
626202408
626200781
626281457
626203632
626200319
626196783
626209587
626202530
626221984
626236632
626240922
626221575
626193667
626196147
626357475
626201484
626209637
626191037
626237251
...

result:

ok 50000 lines

Test #7:

score: -100
Wrong Answer
time: 91ms
memory: 4416kb

input:

100000 100000
408955074 826956417 701304685 581625834 949621992 368783244 994452559 15165521 509385486 202776927 176245918 114961790 593275666 396884961 828315109 874631453 542662516 575542309 479957124 380751420 712708199 246976317 507231011 379597065 514719061 64484505 60035132 476324867 388329012...

output:

-1800035021
-1800016379
-1799911953
-1799871671
-1799997213
-1800037867
-1800005965
-1800036712
-1800014107
-1800013537
-1800033695
-1800044267
-1800013432
-1799977207
-1800012839
-1800023883
-1800038456
-1800019740
-1800003000
-1800008821
-1799977201
-1800007779
-1800045732
-1800041432
-1799957125
...

result:

wrong answer 1st lines differ - expected: '2494932275', found: '-1800035021'