Submission #148004

# Submission time Handle Problem Language Result Execution time Memory
148004 2019-08-31T11:00:17 Z dacin21 Vision Program (IOI19_vision) C++14
100 / 100
22 ms 2296 KB
#include "vision.h"

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



using ll = long long;
using ull = unsigned long long;
using fl = long double;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename S, typename T>
void xmin(S&a, T const&b){if(b<a) a=b;}
template<typename S, typename T>
void xmax(S&a, T const&b){if(b>a) a=b;}

template<bool enabled>
struct Debug{
    template<typename S, typename T = void> struct Tag_Printable : false_type {};
    template<typename S> struct Tag_Printable<S, decltype((void)(cerr << declval<S>()))> : true_type {};
    template<typename S, typename T = void> struct Tag_Iterable: false_type {};
    template<typename S> struct Tag_Iterable<S, decltype((void)(begin(declval<S>()), end(declval<S>())))> : true_type {};
    template<typename T, typename... Args>
    Debug& print(T const&x, true_type, Args...){
        #ifdef LOCAL_RUN
        if(enabled){
            cerr << boolalpha << x;
        }
        #endif // LOCAL_RUN
        return *this;
    }
    template<typename T>
    Debug& print(T const&x, false_type, true_type){
        *this << "[";
        bool first = true;
        for(auto &e:x){
            if(!first) *this << ", ";
            *this << e;
            first = false;
        }
        return *this << "]";
    }
    template<typename S, typename T>
    Debug& print(pair<S, T> const&x, false_type, false_type){
        return *this << "(" << x.first << ", " << x.second << ")";
    }
    template<typename T>
    Debug& operator<<(T const&x){
        return print(x, Tag_Printable<T>{}, Tag_Iterable<T>{});
    }
};
 Debug<true> debug;
// Debug<false> debug; // disable debug printing
#define named(x) string(#x) << " : " <<  x


const int m = __lg(400)+1;

const int ZERO = -1, ONE = -2;

void remove_consts(vector<int> &v){
    v.erase(partition(v.begin(), v.end(), [&](int const&a){return a>=0;}), v.end());
}

int better_and(vector<int> v){
    for(auto const&e:v){
        if(e==ZERO) return ZERO;
    }
    remove_consts(v);
    if(v.empty()) return ONE;
    return add_and(v);
}
int better_or(vector<int> v){
    for(auto const&e:v){
        if(e==ONE) return ONE;
    }
    remove_consts(v);
    if(v.empty()) return ZERO;
    return add_or(v);
}
int better_xor(vector<int> v){
    int flips = 0;
    for(auto const&e:v){
        if(e==ONE) ++flips;
    }
    remove_consts(v);
    if(v.empty()) return flips%2 ? ONE : ZERO;
    return flips%2 ? add_not(add_xor(v)) : add_xor(v);
}
int better_not(int a){
    return a==ONE ? ZERO : a==ZERO ? ONE : add_not(a);
}
#define add_not better_not
#define add_or better_or
#define add_and better_and
#define add_xor better_xor


int add_majority(int a, int b, int c){
    return add_or({add_and({a, b}), add_and({b, c}), add_and({c, a})});
}
vector<int> build_adder(vector<int> v, vector<int> w){
    assert(v.size() == w.size());
    const int n = v.size();
    vector<int> ret(n);
    ret[0] = add_xor({v[0], w[0]});
    int carry = add_and({v[0], w[0]});
    for(int i=1;i<n;++i){
        ret[i] = add_xor({v[i], w[i], carry});
        carry = add_majority(v[i], w[i], carry);
    }
    //ret[n] = carry;
    return ret;
}

vector<int> build_bits(vector<int> v){
    const int n = v.size();
    vector<int> ret(m, ZERO);
    for(int j=0;j<m;++j){
        vector<int> tmp;
        for(int i=0;i<n;++i){
            if((i>>j)&1) tmp.push_back(v[i]);
        }
        ret[j] = add_or(tmp);
    }
    debug << "Bits: " << ret << "\n";
    return ret;
}

array<vector<int>, 2> first_last(vector<int> a){
    const int n = a.size();
    array<vector<int>, 2> ret{};
    for(auto &e:ret) e.resize(n);
    for(int i=0;i<n;++i){
        vector<int> tmp;
        for(int j=0;j<i;++j){
            tmp.push_back(a[j]);
        }
        ret[0][i] = add_and({a[i], add_not(add_or(tmp))});
    }
    for(int i=0;i<n;++i){
        vector<int> tmp;
        for(int j=i+1;j<n;++j){
            tmp.push_back(a[j]);
        }
        ret[1][i] = add_and({a[i], add_not(add_or(tmp))});
    }
    return ret;
}

vector<int> sub(vector<int> a, vector<int> b){
    for(auto &e:b) e = add_not(e);
    vector<int> one(a.size(), ZERO);
    one[0] = ONE;
    auto c = build_adder(a, b);
    return build_adder(c, one);
}

void construct_network(int H, int W, int K) {
    const int X = H, Y = W;
    auto ind = [&](int x, int y){
        assert(0 <= x && x < X);
        assert(0 <= y && y < Y);
        return x*Y+y;
    };
    vector<int> rows(X), cols(Y);
    for(int i=0;i<X;++i){
        vector<int> tmp;
        for(int j=0;j<Y;++j){
            tmp.push_back(ind(i, j));
        }
        rows[i] = add_xor(tmp);
    }
    for(int j=0;j<Y;++j){
        vector<int> tmp;
        for(int i=0;i<X;++i){
            tmp.push_back(ind(i, j));
        }
        cols[j] = add_xor(tmp);
    }
    auto R = first_last(rows);
    auto C = first_last(cols);
    vector<int> dx = sub(build_bits(R[1]), build_bits(R[0]));
    vector<int> dy = sub(build_bits(C[1]), build_bits(C[0]));
    vector<int> d = build_adder(dx, dy);
    debug << dx << " " << dy << "\n";
    for(int i=0;i<(int)d.size();++i){
        if((~K>>i)&1){
            d[i] = add_not(d[i]);
        }
    }
    debug << named(d) << "\n";
    int ret = add_and(d);
    assert(ret >= 0);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 452 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 452 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 452 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 5 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 348 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 3 ms 376 KB Output is correct
37 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 452 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 5 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 348 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 3 ms 376 KB Output is correct
37 Correct 3 ms 376 KB Output is correct
38 Correct 7 ms 760 KB Output is correct
39 Correct 4 ms 504 KB Output is correct
40 Correct 4 ms 504 KB Output is correct
41 Correct 5 ms 632 KB Output is correct
42 Correct 5 ms 632 KB Output is correct
43 Correct 7 ms 888 KB Output is correct
44 Correct 8 ms 888 KB Output is correct
45 Correct 7 ms 760 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 8 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 760 KB Output is correct
2 Correct 7 ms 760 KB Output is correct
3 Correct 8 ms 760 KB Output is correct
4 Correct 7 ms 760 KB Output is correct
5 Correct 7 ms 780 KB Output is correct
6 Correct 7 ms 760 KB Output is correct
7 Correct 7 ms 760 KB Output is correct
8 Correct 7 ms 760 KB Output is correct
9 Correct 8 ms 760 KB Output is correct
10 Correct 7 ms 760 KB Output is correct
11 Correct 7 ms 748 KB Output is correct
12 Correct 7 ms 760 KB Output is correct
13 Correct 7 ms 760 KB Output is correct
14 Correct 7 ms 764 KB Output is correct
15 Correct 7 ms 760 KB Output is correct
16 Correct 7 ms 760 KB Output is correct
17 Correct 7 ms 760 KB Output is correct
18 Correct 7 ms 760 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 7 ms 760 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 4 ms 632 KB Output is correct
4 Correct 4 ms 632 KB Output is correct
5 Correct 4 ms 508 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 6 ms 632 KB Output is correct
9 Correct 6 ms 632 KB Output is correct
10 Correct 6 ms 760 KB Output is correct
11 Correct 6 ms 632 KB Output is correct
12 Correct 6 ms 760 KB Output is correct
13 Correct 7 ms 760 KB Output is correct
14 Correct 7 ms 760 KB Output is correct
15 Correct 7 ms 760 KB Output is correct
16 Correct 7 ms 760 KB Output is correct
17 Correct 8 ms 760 KB Output is correct
18 Correct 7 ms 760 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 13 ms 1400 KB Output is correct
21 Correct 13 ms 1400 KB Output is correct
22 Correct 14 ms 1400 KB Output is correct
23 Correct 13 ms 1404 KB Output is correct
24 Correct 13 ms 1400 KB Output is correct
25 Correct 13 ms 1400 KB Output is correct
26 Correct 13 ms 1400 KB Output is correct
27 Correct 21 ms 2168 KB Output is correct
28 Correct 21 ms 2168 KB Output is correct
29 Correct 21 ms 2040 KB Output is correct
30 Correct 21 ms 2040 KB Output is correct
31 Correct 21 ms 2168 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2168 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 6 ms 760 KB Output is correct
5 Correct 7 ms 760 KB Output is correct
6 Correct 7 ms 760 KB Output is correct
7 Correct 14 ms 1524 KB Output is correct
8 Correct 13 ms 1400 KB Output is correct
9 Correct 21 ms 2168 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 452 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 5 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 348 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 3 ms 376 KB Output is correct
37 Correct 3 ms 376 KB Output is correct
38 Correct 7 ms 760 KB Output is correct
39 Correct 4 ms 504 KB Output is correct
40 Correct 4 ms 504 KB Output is correct
41 Correct 5 ms 632 KB Output is correct
42 Correct 5 ms 632 KB Output is correct
43 Correct 7 ms 888 KB Output is correct
44 Correct 8 ms 888 KB Output is correct
45 Correct 7 ms 760 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 8 ms 888 KB Output is correct
48 Correct 7 ms 760 KB Output is correct
49 Correct 7 ms 760 KB Output is correct
50 Correct 8 ms 760 KB Output is correct
51 Correct 7 ms 760 KB Output is correct
52 Correct 7 ms 780 KB Output is correct
53 Correct 7 ms 760 KB Output is correct
54 Correct 7 ms 760 KB Output is correct
55 Correct 7 ms 760 KB Output is correct
56 Correct 8 ms 760 KB Output is correct
57 Correct 7 ms 760 KB Output is correct
58 Correct 7 ms 748 KB Output is correct
59 Correct 7 ms 760 KB Output is correct
60 Correct 7 ms 760 KB Output is correct
61 Correct 7 ms 764 KB Output is correct
62 Correct 7 ms 760 KB Output is correct
63 Correct 7 ms 760 KB Output is correct
64 Correct 7 ms 760 KB Output is correct
65 Correct 7 ms 760 KB Output is correct
66 Correct 7 ms 760 KB Output is correct
67 Correct 7 ms 760 KB Output is correct
68 Correct 2 ms 376 KB Output is correct
69 Correct 2 ms 376 KB Output is correct
70 Correct 2 ms 252 KB Output is correct
71 Correct 2 ms 256 KB Output is correct
72 Correct 4 ms 632 KB Output is correct
73 Correct 4 ms 632 KB Output is correct
74 Correct 4 ms 508 KB Output is correct
75 Correct 5 ms 504 KB Output is correct
76 Correct 4 ms 504 KB Output is correct
77 Correct 6 ms 632 KB Output is correct
78 Correct 6 ms 632 KB Output is correct
79 Correct 6 ms 760 KB Output is correct
80 Correct 6 ms 632 KB Output is correct
81 Correct 6 ms 760 KB Output is correct
82 Correct 7 ms 760 KB Output is correct
83 Correct 7 ms 760 KB Output is correct
84 Correct 7 ms 760 KB Output is correct
85 Correct 7 ms 760 KB Output is correct
86 Correct 8 ms 760 KB Output is correct
87 Correct 7 ms 760 KB Output is correct
88 Correct 7 ms 760 KB Output is correct
89 Correct 13 ms 1400 KB Output is correct
90 Correct 13 ms 1400 KB Output is correct
91 Correct 14 ms 1400 KB Output is correct
92 Correct 13 ms 1404 KB Output is correct
93 Correct 13 ms 1400 KB Output is correct
94 Correct 13 ms 1400 KB Output is correct
95 Correct 13 ms 1400 KB Output is correct
96 Correct 21 ms 2168 KB Output is correct
97 Correct 21 ms 2168 KB Output is correct
98 Correct 21 ms 2040 KB Output is correct
99 Correct 21 ms 2040 KB Output is correct
100 Correct 21 ms 2168 KB Output is correct
101 Correct 2 ms 376 KB Output is correct
102 Correct 2 ms 256 KB Output is correct
103 Correct 21 ms 2168 KB Output is correct
104 Correct 2 ms 376 KB Output is correct
105 Correct 4 ms 504 KB Output is correct
106 Correct 6 ms 760 KB Output is correct
107 Correct 7 ms 760 KB Output is correct
108 Correct 7 ms 760 KB Output is correct
109 Correct 14 ms 1524 KB Output is correct
110 Correct 13 ms 1400 KB Output is correct
111 Correct 21 ms 2168 KB Output is correct
112 Correct 2 ms 256 KB Output is correct
113 Correct 2 ms 256 KB Output is correct
114 Correct 21 ms 2168 KB Output is correct
115 Correct 7 ms 760 KB Output is correct
116 Correct 7 ms 760 KB Output is correct
117 Correct 13 ms 1400 KB Output is correct
118 Correct 13 ms 1528 KB Output is correct
119 Correct 21 ms 2168 KB Output is correct
120 Correct 21 ms 2168 KB Output is correct
121 Correct 21 ms 2168 KB Output is correct
122 Correct 21 ms 2168 KB Output is correct
123 Correct 21 ms 2168 KB Output is correct
124 Correct 22 ms 2080 KB Output is correct
125 Correct 21 ms 2140 KB Output is correct
126 Correct 21 ms 2168 KB Output is correct
127 Correct 21 ms 2296 KB Output is correct
128 Correct 21 ms 2168 KB Output is correct