QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99787#4802. Ternary Searchckiseki#WA 2ms5452kbC++202.2kb2023-04-23 18:25:362023-04-23 18:25:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-23 18:25:39]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5452kb
  • [2023-04-23 18:25:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define orange(a...) orange_(#a, a)
#define debug(a...) debug_(#a, a)
template <typename i>
void orange_(const char * s, i l, i r) {
    cerr << "\e[1;32m[ " << s << " ] = [ ";
    for (int f = 0; l != r; l++)
        cerr << (f++ ? " " : "") << *l;
    cerr << " ]\e[0m\n";
}
template <typename ...T>
void debug_(const char * s, T ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int cnt = sizeof...(T);
    (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif


const int maxn = 200025;

struct Fenwick {
    int b[maxn];
    void add(int p, int v) {
        for (++p; p < maxn; p += p & -p)
            b[p] += v;
    }
    int query(int p) {
        int r = 0;
        for (++p; p > 0; p -= p & -p)
            r += b[p];
        return r;
    }
} lef[2], rig[2];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    cin >> N;

    vector<int> a(N);
    vector<array<int,2>> val(N);
    for (int i = 0; i < N; i++)
        cin >> a[i];
    auto u = a;
    sort(u.begin(), u.end());
    for (int i = 0; i < N; i++) {
        val[i][0] = lower_bound(u.begin(), u.end(), a[i]) - u.begin();
        val[i][1] = u.end() - lower_bound(u.begin(), u.end(), a[i]);
    }

    int it[2] = {0, 0};
    int64_t current_cost[2] = {0, 0};

    for (int i = 0; i < N; i++) {
        for (int c: {0, 1}) {
            current_cost[c] += rig[c].query(val[i][c]);
            rig[c].add(val[i][c], 1);

            while (it[c] < i) {
                int64_t diff = lef[c].query(maxn - 2) - lef[c].query(val[it[c]][c]) -
                    (rig[c].query(maxn - 2) - rig[c].query(val[it[c]][c]));
                if (diff <= 0) {
                    current_cost[c] += diff;
                    lef[c].add(val[it[c]][c], 1);
                    rig[c].add(val[it[c]][c], -1);
                    ++it[c];
                } else {
                    break;
                }
            }

            debug(current_cost[0], current_cost[1]);
        }
        cout << min(current_cost[0], current_cost[1]) << '\n';
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

9
11
4
5
14
1
9
19
8
10

output:

0
0
0
0
2
3
3
6
7

result:

ok 9 numbers

Test #2:

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

input:

1
167959139

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

10
802030518
983359971
96204862
640274071
796619138
71550121
799843967
598196518
402690754
446173607

output:

0
0
0
1
1
3
3
5
7
9

result:

ok 10 numbers

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 5452kb

input:

200
280383943
994080326
443906342
107926501
414713819
45732490
370693588
928126378
581740643
189811400
142817640
218386999
447997927
640003704
637428425
151698689
209462128
155599305
390080477
958251194
515653943
751042490
1186761
443171900
968975479
829084542
462837689
157692260
607049612
606116398...

output:

0
0
0
0
1
1
3
4
5
8
12
15
17
18
19
25
30
35
38
38
41
42
54
60
60
62
69
82
88
95
112
115
124
126
139
148
152
164
169
180
187
193
212
223
226
244
244
252
265
274
288
311
320
333
345
367
392
407
425
434
457
470
473
491
526
542
564
582
597
623
651
662
669
669
696
708
741
759
784
808
837
844
882
863
896
...

result:

wrong answer 28th numbers differ - expected: '81', found: '82'