CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Vaihdot
Sender:kluopaja
Submission time:2020-09-06 12:48:40 +0300
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED13
#2ACCEPTED23
#3ACCEPTED64
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.01 s1, 2, 3details
#6ACCEPTED0.01 s1, 2, 3details
#7ACCEPTED0.01 s1, 2, 3details
#8ACCEPTED0.01 s1, 2, 3details
#9ACCEPTED0.01 s2, 3details
#10ACCEPTED0.01 s2, 3details
#11ACCEPTED0.01 s2, 3details
#12ACCEPTED0.01 s2, 3details
#13ACCEPTED0.01 s2, 3details
#14ACCEPTED0.01 s2, 3details
#15ACCEPTED0.01 s2, 3details
#16ACCEPTED0.01 s2, 3details
#17ACCEPTED0.72 s3details
#18ACCEPTED0.72 s3details
#19ACCEPTED0.72 s3details
#20ACCEPTED0.72 s3details
#21ACCEPTED0.72 s3details
#22ACCEPTED0.73 s3details
#23ACCEPTED0.74 s3details
#24ACCEPTED0.74 s3details

Compiler report

input/code.cpp: In function 'int consume(std::vector<int>&)':
input/code.cpp:9:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(last < v.size()) {
           ~~~~~^~~~~~~~~~
input/code.cpp:10:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < v.size(); ++i) {
                        ~~^~~~~~~~~~
input/code.cpp: In function 'int consume2(std::vector<int>&)':
input/code.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < v.size(); ++i) {
                    ~~^~~~~~~~~~
input/code.cpp:23:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i+1 < pos.size(); ++i) {
                    ~~~~^~~~~~~~~~~~
input/code.cpp: In function 'std::vector<int> brute(std::vector<int>)':
input/code.cpp:29:22: warning: comparison between signed and unsigned integer exp...

Code

#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;

int consume(vector<int>& v) {
    int last = 0;
    int rounds = 0;
    while(last < v.size()) {
        for(int i = 0; i < v.size(); ++i) {
            if(v[i] == last+1) ++last;
        }
        ++rounds;
    }
    return rounds;
}
int consume2(vector<int>& v) {
    vector<int> pos(v.size());
    for(int i = 0; i < v.size(); ++i) {
        pos[v[i]] = i;
    }
    int inversion_cnt = 0;
    for(int i = 0; i+1 < pos.size(); ++i) {
        if(pos[i] > pos[i+1]) ++inversion_cnt;
    }
    return inversion_cnt+1;
}
vector<int> brute(vector<int> v) {
    for(int i = 0; i < v.size(); ++i) {
        --v[i];
    }
    int min_ans = 1e9;
    int min_cnt = 0;
    int max_ans = 0;
    vector<pair<int, int> > swaps;
    for(int i = 0; i < v.size(); ++i) {
        for(int j = 0; j < i; ++j) {
            swap(v[i], v[j]);
            int rounds = consume2(v);
            swap(v[i], v[j]);
            max_ans = max(max_ans, rounds);
            if(rounds < min_ans) {
                min_ans = rounds;
                min_cnt = 1;
                swaps.clear();
                swaps.push_back({i, j});
            }
            else if(rounds == min_ans) {
                ++min_cnt;
                swaps.push_back({i, j});
            }
        }
    }
    //for(auto x: swaps) cout<<x.first<<' '<<x.second<<'\n';
    return {min_ans, min_cnt, consume2(v), max_ans};
}
struct Event {
    int pos;
    int element;
    int type;
};
bool operator< (const Event& a, const Event& b) {
    if(a.pos == b.pos) {
        return a.type < b.type;
    }
    return a.pos < b.pos;
}
struct SwapEffect {
    int start;
    int end;
    int type;
};
vector<SwapEffect> getSwapEffects(int i, vector<int>& v, vector<int>& pos) {
    vector<SwapEffect> out;
    int p0 = pos[v[i]-1];
    int p1 = pos[v[i]];
    int p2 = pos[v[i]+1];
    int n = v.size();
    // 000+++000
    if(p0 < p2) {
        if(p0 < p1 && p1 < p2) {
            out.push_back({0, p0, 1});
            out.push_back({p0+1, p2, 0});
            out.push_back({p2+1, n-1, 1});
        }
        else {
            out.push_back({0, p0, 0});
            out.push_back({p0+1, p2, -1});
            out.push_back({p2+1, n-1, 0});
        }
    }
    // +++000+++
    else {
        if(p2 < p1 && p1 < p0) {
            out.push_back({0, p2, -1});
            out.push_back({p2+1, p0, 0});
            out.push_back({p0+1, n-1, -1});
        }
        else {
            out.push_back({0, p2, 0});
            out.push_back({p2+1, p0, 1});
            out.push_back({p0+1, n-1, 0});
        }
    }
    return out;
}
class BitVector {
private:
    ull *v;
public:
    BitVector(int n) {
        v = new ull[(n+63)/64]();
    }
    bool get(int pos) {
        return !!(v[pos/64]&(1ull<<(pos&63)));
    }
    void set(int pos, bool value) {
        v[pos/64] ^= (-value^v[pos/64])&(1ull<<(pos&63));
    }
    int sum(int start, int end) {
        --end;
        if(start > end) return 0;
        if(start/64 == end/64) {
            ull tmp = (v[start/64]&(-1ull>>(63-end%64))) & (-1ull<<(start%64));
            return __builtin_popcountll(tmp);
        }
        ull tmp = v[end/64]&(-1ull>>(63-end%64));
        int s = __builtin_popcountll(tmp);//v[start/64]&(-1ull>>(64-end
        tmp = v[start/64]&(-1ull<<(start%64));
        s += __builtin_popcountll(tmp);//v[start/64]&(-1ull>>(64-end
        for(int i = start/64+1; i < end/64; ++i) {
            s += __builtin_popcountll(v[i]);
            //s += !!(v[i/64]&(1ull<<(i%64)));
        }
        return s;
    }
    ~BitVector() {
        delete[] v;
    }
};
vector<ull> solve(vector<int> v) {
    ull ans[5]{};
    vector<Event> events;
    v.insert(v.begin(), 0);
    v.push_back(v.size());
    vector<int> pos(v.size());
    //cout<<"v: "<<endl;
    for(int i = 0; i < v.size(); ++i) {
    //    cout<<v[i]<<' ';
        pos[v[i]] = i;
    }
    //cout<<'\n';
    int n = v.size();
    for(int i = 1; i+1 < v.size(); ++i) {
        auto effects = getSwapEffects(i, v, pos);
      //  cout<<"element: "<<v[i]<<endl;
        for(auto x: effects) {
     //       cout<<"effect: "<<x.start<<' '<<x.end<<' '<<x.type<<endl;
            if(x.type != 0) {
                events.push_back({x.start, i, x.type});
                events.push_back({x.end, i, -x.type});
            }
        }
    }
    sort(events.begin(), events.end());
    /*
    cout<<"events: "<<endl;
    for(auto x: events) {
        cout<<x.pos<<' '<<x.element<<' '<<x.type<<endl;
    }
    cout<<"testing :"<<(events[2]<events[3])<<endl;
    */
    BitVector minus(v.size());
    BitVector plus(v.size());
    int total_minus = 0;
    int total_plus = 0;
    int e_pos = 0;
    for(int i = 1; i+1 < v.size(); ++i) {
         //cout<<'i'<<i<<endl;
        while(e_pos < events.size() && events[e_pos].pos <= i) {
            /*
             cout<<"epos "<<e_pos<<' '<<events.size()<<endl;
             cout<<events[e_pos].element<<endl;
             cout<<events[e_pos].type<<endl;
             cout<<v.size()<<endl;
             */
            if(events[e_pos].type == -1) {
                if(plus.get(events[e_pos].element)) {
                    plus.set(events[e_pos].element,  0);
                    --total_plus;
                }
                else {
                    minus.set(events[e_pos].element, 1);
                    ++total_minus;
                }
            }
            if(events[e_pos].type == 1) {
                if(minus.get(events[e_pos].element)) {
                    minus.set(events[e_pos].element, 0);
                    --total_minus;
                }
                else {
                    plus.set(events[e_pos].element, 1);
                    ++total_plus;
                }
            }
            ++e_pos;
        }
        auto effects = getSwapEffects(i, v, pos);
        for(auto x: effects) {
            x.start = max(i+1, x.start);
            x.end = max(x.start, x.end);
            int plus_sum = plus.sum(x.start, x.end);
            int minus_sum = minus.sum(x.start, x.end);
           // cout<<v[i]<<endl;
           // cout<<"plus and minus: "<<plus_sum<<' '<<minus_sum<<endl;
           // cout<<x.type<<' '<<x.start<<' '<<x.end<<endl;
            ans[2+x.type-1] += minus_sum;
            ans[2+x.type] += x.end - x.start - plus_sum - minus_sum;
            ans[2+x.type+1] += plus_sum;
        }
    }
    //cout<<ans[0]<<' '<<ans[1]<<' '<<ans[2]<<endl;
    for(int i = 1; i+1 < v.size(); ++i) {
        if(v[i]+2 == v.size()) continue;
        int prev = 0;
        for(int j = -1; j <= 1; ++j) {
            prev += pos[v[i]+j+1] < pos[v[i]+j];
        }
        swap(pos[v[i]], pos[v[i]+1]);
        int after = 0;
        for(int j = -1; j <= 1; ++j) {
            after += pos[v[i]+j+1] < pos[v[i]+j];
        }
        swap(pos[v[i]], pos[v[i]+1]);
        //cout<<prev<<' '<<after<<endl;
        ++ans[2+after-prev];
    }
    for(int i = 0; i < 5; ++i) {
        if(ans[i] > 0) {
            vector<ull> out = {(ull)consume2(v)+i-2, ans[i]};
            return out;
        }
    }
    return {};
}
vector<int> read() {
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i = 0; i < n; ++i) {
        cin>>v[i];
    }
    return v;
}
int main() {
    auto ans = solve(read());
    cout<<ans[0]<<' '<<ans[1]<<endl;
    return 0;
    vector<int> v(200000);
    for(int i = 0; i < v.size(); ++i) {
        v[i] = i+1;
    }
    // mt19937 mt(chrono::system_clock::now().time_since_epoch().count());
    mt19937 mt(1337);
    for(int i = 0; i < 10; ++i) {
        shuffle(v.begin(), v.end(), mt);
        //auto ans = brute(v);
        auto ans2 = solve(v);
        cout<<ans2[0]<<' '<<ans2[1]<<endl;
    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 99

user output
2 99

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
100 99 98 97 96 95 94 93 92 91...

correct output
98 4851

user output
98 4851

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
1 2 88 4 5 6 7 8 9 10 11 12 13...

correct output
16 5

user output
16 5

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
51 48 74 70 45 71 24 88 55 99 ...

correct output
49 131

user output
49 131

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
45 67 29 62 70 77 41 74 52 95 ...

correct output
52 268

user output
52 268

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
47 98 2 75 6 21 84 8 4 89 27 9...

correct output
48 149

user output
48 149

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
73 68 17 94 71 63 61 13 58 10 ...

correct output
47 116

user output
47 116

Test 8

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
17 16 45 94 6 1 36 81 31 13 51...

correct output
45 95

user output
45 95

Test 9

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 4999

user output
2 4999

Test 10

Group: 2, 3

Verdict: ACCEPTED

input
5000
5000 4999 4998 4997 4996 4995 ...

correct output
4998 12492501

user output
4998 12492501

Test 11

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
19 10

user output
19 10

Test 12

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 264 8 9 10 11 12 1...

correct output
190 96

user output
190 96

Test 13

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 7 8 9 2400 11 12 1...

correct output
1378 27938

user output
1378 27938

Test 14

Group: 2, 3

Verdict: ACCEPTED

input
5000
4012 2 4820 4208 1868 1728 362...

correct output
2511 436307

user output
2511 436307

Test 15

Group: 2, 3

Verdict: ACCEPTED

input
5000
3877 3972 1112 3669 1959 4640 ...

correct output
2497 417065

user output
2497 417065

Test 16

Group: 2, 3

Verdict: ACCEPTED

input
5000
2774 998 4525 2884 487 1995 41...

correct output
2518 426448

user output
2518 426448

Test 17

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 199999

user output
2 199999

Test 18

Group: 3

Verdict: ACCEPTED

input
200000
200000 199999 199998 199997 19...

correct output
199998 19999700001

user output
199998 19999700001

Test 19

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
19 10

user output
19 10

Test 20

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
199 100

user output
199 100

Test 21

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1979 1030

user output
1979 1030

Test 22

Group: 3

Verdict: ACCEPTED

input
200000
1 2 184153 4 5 6 7 8 164545 10...

correct output
18081 477187

user output
18081 477187

Test 23

Group: 3

Verdict: ACCEPTED

input
200000
151013 68675 119105 58292 3335...

correct output
86328 318722426

user output
86328 318722426

Test 24

Group: 3

Verdict: ACCEPTED

input
200000
11562 33356 106752 170825 2723...

correct output
99873 663048119

user output
99873 663048119