Submission #697149

# Submission time Handle Problem Language Result Execution time Memory
697149 2023-02-08T16:06:46 Z whqkrtk04 Painting Walls (APIO20_paint) C++17
28 / 100
1249 ms 524288 KB
#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
template <typename T>
ostream& operator<<(ostream& os, const deque<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }

vector<int> evaluate(int M, deque<int> &chk, vector<int> &can) {
    vector<int> ret;
    if(chk.size() > can.size()) return ret;
    if(chk.empty()) {
        for(int i = 0; i < M; i++) ret.push_back(i);
        return ret;
    }
    for(int i = 0; i < can.size(); i++) {
        int diff = ((can[i]-chk[0])%M+M)%M;
        bool valid = true;
        for(int j = 0; j < chk.size(); j++) {
            int tmp = ((chk[j]+diff)%M+M)%M;
            auto iter = lower_bound(can.begin(), can.end(), tmp);
            if(iter == can.end() || *iter != tmp) valid = false;
        }
        if(valid) ret.push_back(diff);
    }
    return ret;
}

template <typename node_seg, typename node_query = node_seg, typename index_t = int>
class Segtree {
	private:
	const size_t n;
	std::vector<node_seg> seg;

	void init(const size_t i, const index_t s, const index_t e, const std::vector<node_seg> &A) {
		if(s+1 == e) seg[i] = A[s];
		else {
			init(i<<1, s, s+e>>1, A);
			init(i<<1|1, s+e>>1, e, A);
			seg[i] = seg[i<<1]+seg[i<<1|1];
		}
	}

	void update(const size_t i, const index_t s, const index_t e, const index_t j, const node_query &x) {
		if(j >= e || s > j) return;
		if(s+1 == e) seg[i] += x;
		else {
			update(i<<1, s, s+e>>1, j, x);
			update(i<<1|1, s+e>>1, e, j, x);
			seg[i] = seg[i<<1]+seg[i<<1|1];
		}
	}

	node_seg query(const size_t i, const index_t s, const index_t e, const index_t l, const index_t r) const {
		if(e <= l || r <= s) return node_seg::inf();
		if(l <= s && e <= r) return seg[i];
		return query(i<<1, s, s+e>>1, l, r)+query(i<<1|1, s+e>>1, e, l, r);
	}

	public:
	Segtree(const int n) : n(n) {
		seg.resize(4*n, node_seg::inf());
	}
	Segtree(const std::vector<node_seg> &A) : n(A.size()) {
		seg.resize(4*n, node_seg::inf());
		init(1, 0, n, A);
	}
	void update(const index_t j, const node_query &x) { update(1, 0, n, j, x); }
	node_seg query(const index_t l, const index_t r) const { return query(1, 0, n, l, r); }
};

struct min_node {
    int x;
    static min_node inf() { return {INF}; }
    min_node operator+(const min_node &y) { return {min(x, y.x)}; }
    void operator+=(const min_node &y) { x = min(x, y.x); }
};

int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
    vector<deque<int>> chk(K);
    vector<vector<int>> verdict(K), can(K);
    vector<int> cnt(M, 0);
    vector<bool> can_paint(N, false);
    int macnt = 0;
    for(int i = 0; i < M; i++) {
        chk[C[i]].push_back(i);
        for(auto j : B[i]) can[j].push_back(i);
    }
    //cout << " " << chk;
    //cout << " " << can;
    for(int i = 0; i < K; i++) {
        verdict[i] = evaluate(M, chk[i], can[i]);
        for(auto j : verdict[i]) if(++cnt[j] == K) macnt++;
    }
    //cout << M-1 << "\n" << " " << verdict;
    can_paint[M-1] = macnt > 0;
    for(int i = M; i < N; i++) {
        for(auto j : verdict[C[i-M]]) if(cnt[j]-- == K) macnt--;
        for(auto j : verdict[C[i]]) if(cnt[j]-- == K) macnt--;
        chk[C[i-M]].pop_front();
        chk[C[i]].push_back(i);
        verdict[C[i-M]] = evaluate(M, chk[C[i-M]], can[C[i-M]]);
        verdict[C[i]] = evaluate(M, chk[C[i]], can[C[i]]);
        for(auto j : verdict[C[i-M]]) if(++cnt[j] == K) macnt++;
        for(auto j : verdict[C[i]]) if(++cnt[j] == K) macnt++;
        //cout << i << "\n" << " " << verdict;
        can_paint[i] = macnt > 0;
    }
    //cout << can_paint << "\n";
    Segtree<min_node> dp(vector<min_node>(N, min_node::inf()));
    if(can_paint[M-1]) dp.update(M-1, {1});
    for(int i = M; i < N; i++) {
        if(can_paint[i]) {
            int tmp = dp.query(i-M, i).x;
            dp.update(i, {++tmp});
        }
    }
    int ans = dp.query(N-1, N).x;
    if(ans == INF) return -1;
    else return ans;
}

Compilation message

paint.cpp: In function 'std::vector<int> evaluate(int, std::deque<int>&, std::vector<int>&)':
paint.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int i = 0; i < can.size(); i++) {
      |                    ~~^~~~~~~~~~~~
paint.cpp:31:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for(int j = 0; j < chk.size(); j++) {
      |                        ~~^~~~~~~~~~~~
paint.cpp: In instantiation of 'void Segtree<node_seg, node_query, index_t>::init(size_t, index_t, index_t, const std::vector<_Tp>&) [with node_seg = min_node; node_query = min_node; index_t = int; size_t = long unsigned int]':
paint.cpp:78:3:   required from 'Segtree<node_seg, node_query, index_t>::Segtree(const std::vector<_Tp>&) [with node_seg = min_node; node_query = min_node; index_t = int]'
paint.cpp:122:62:   required from here
paint.cpp:50:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |    init(i<<1, s, s+e>>1, A);
      |                  ~^~
paint.cpp:51:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   51 |    init(i<<1|1, s+e>>1, e, A);
      |                 ~^~
paint.cpp: In instantiation of 'void Segtree<node_seg, node_query, index_t>::update(size_t, index_t, index_t, index_t, const node_query&) [with node_seg = min_node; node_query = min_node; index_t = int; size_t = long unsigned int]':
paint.cpp:80:60:   required from 'void Segtree<node_seg, node_query, index_t>::update(index_t, const node_query&) [with node_seg = min_node; node_query = min_node; index_t = int]'
paint.cpp:123:42:   required from here
paint.cpp:60:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   60 |    update(i<<1, s, s+e>>1, j, x);
      |                    ~^~
paint.cpp:61:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |    update(i<<1|1, s+e>>1, e, j, x);
      |                   ~^~
paint.cpp: In instantiation of 'node_seg Segtree<node_seg, node_query, index_t>::query(size_t, index_t, index_t, index_t, index_t) const [with node_seg = min_node; node_query = min_node; index_t = int; size_t = long unsigned int]':
paint.cpp:81:71:   required from 'node_seg Segtree<node_seg, node_query, index_t>::query(index_t, index_t) const [with node_seg = min_node; node_query = min_node; index_t = int]'
paint.cpp:126:38:   required from here
paint.cpp:69:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   69 |   return query(i<<1, s, s+e>>1, l, r)+query(i<<1|1, s+e>>1, e, l, r);
      |                         ~^~
paint.cpp:69:54: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   69 |   return query(i<<1, s, s+e>>1, l, r)+query(i<<1|1, s+e>>1, e, l, r);
      |                                                     ~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 284 ms 173864 KB Output is correct
14 Correct 278 ms 173940 KB Output is correct
15 Correct 278 ms 173864 KB Output is correct
16 Correct 281 ms 174036 KB Output is correct
17 Correct 283 ms 173904 KB Output is correct
18 Correct 300 ms 173824 KB Output is correct
19 Correct 277 ms 173900 KB Output is correct
20 Correct 278 ms 173864 KB Output is correct
21 Correct 283 ms 173936 KB Output is correct
22 Correct 296 ms 178300 KB Output is correct
23 Correct 300 ms 178368 KB Output is correct
24 Correct 305 ms 178380 KB Output is correct
25 Correct 8 ms 852 KB Output is correct
26 Correct 8 ms 876 KB Output is correct
27 Correct 7 ms 880 KB Output is correct
28 Correct 5 ms 884 KB Output is correct
29 Correct 5 ms 828 KB Output is correct
30 Correct 5 ms 852 KB Output is correct
31 Runtime error 1249 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 284 ms 173864 KB Output is correct
14 Correct 278 ms 173940 KB Output is correct
15 Correct 278 ms 173864 KB Output is correct
16 Correct 281 ms 174036 KB Output is correct
17 Correct 283 ms 173904 KB Output is correct
18 Correct 300 ms 173824 KB Output is correct
19 Correct 277 ms 173900 KB Output is correct
20 Correct 278 ms 173864 KB Output is correct
21 Correct 283 ms 173936 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 300 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 6 ms 212 KB Output is correct
29 Correct 2 ms 304 KB Output is correct
30 Correct 2 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 2 ms 212 KB Output is correct
33 Correct 211 ms 148620 KB Output is correct
34 Correct 163 ms 111068 KB Output is correct
35 Correct 124 ms 97384 KB Output is correct
36 Correct 239 ms 170412 KB Output is correct
37 Correct 214 ms 137900 KB Output is correct
38 Correct 51 ms 66124 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 186 ms 131020 KB Output is correct
41 Correct 1 ms 724 KB Output is correct
42 Correct 148 ms 117120 KB Output is correct
43 Correct 109 ms 91000 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 10 ms 212 KB Output is correct
46 Correct 4 ms 212 KB Output is correct
47 Correct 4 ms 308 KB Output is correct
48 Correct 3 ms 212 KB Output is correct
49 Correct 2 ms 212 KB Output is correct
50 Correct 286 ms 173876 KB Output is correct
51 Correct 295 ms 173840 KB Output is correct
52 Correct 291 ms 173864 KB Output is correct
53 Correct 288 ms 173900 KB Output is correct
54 Correct 287 ms 173900 KB Output is correct
55 Correct 293 ms 173872 KB Output is correct
56 Correct 290 ms 173840 KB Output is correct
57 Correct 2 ms 980 KB Output is correct
58 Correct 286 ms 173872 KB Output is correct
59 Correct 2 ms 1048 KB Output is correct
60 Correct 304 ms 173940 KB Output is correct
61 Correct 3 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 284 ms 173864 KB Output is correct
16 Correct 278 ms 173940 KB Output is correct
17 Correct 278 ms 173864 KB Output is correct
18 Correct 281 ms 174036 KB Output is correct
19 Correct 283 ms 173904 KB Output is correct
20 Correct 300 ms 173824 KB Output is correct
21 Correct 277 ms 173900 KB Output is correct
22 Correct 278 ms 173864 KB Output is correct
23 Correct 283 ms 173936 KB Output is correct
24 Correct 296 ms 178300 KB Output is correct
25 Correct 300 ms 178368 KB Output is correct
26 Correct 305 ms 178380 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 300 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 6 ms 212 KB Output is correct
32 Correct 2 ms 304 KB Output is correct
33 Correct 2 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 2 ms 212 KB Output is correct
36 Correct 211 ms 148620 KB Output is correct
37 Correct 163 ms 111068 KB Output is correct
38 Correct 124 ms 97384 KB Output is correct
39 Correct 239 ms 170412 KB Output is correct
40 Correct 214 ms 137900 KB Output is correct
41 Correct 51 ms 66124 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 186 ms 131020 KB Output is correct
44 Correct 1 ms 724 KB Output is correct
45 Correct 148 ms 117120 KB Output is correct
46 Correct 109 ms 91000 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 10 ms 212 KB Output is correct
49 Correct 4 ms 212 KB Output is correct
50 Correct 4 ms 308 KB Output is correct
51 Correct 3 ms 212 KB Output is correct
52 Correct 2 ms 212 KB Output is correct
53 Correct 286 ms 173876 KB Output is correct
54 Correct 295 ms 173840 KB Output is correct
55 Correct 291 ms 173864 KB Output is correct
56 Correct 288 ms 173900 KB Output is correct
57 Correct 287 ms 173900 KB Output is correct
58 Correct 293 ms 173872 KB Output is correct
59 Correct 290 ms 173840 KB Output is correct
60 Correct 2 ms 980 KB Output is correct
61 Correct 286 ms 173872 KB Output is correct
62 Correct 2 ms 1048 KB Output is correct
63 Correct 304 ms 173940 KB Output is correct
64 Correct 3 ms 980 KB Output is correct
65 Correct 61 ms 320 KB Output is correct
66 Correct 14 ms 212 KB Output is correct
67 Correct 110 ms 304 KB Output is correct
68 Correct 31 ms 316 KB Output is correct
69 Correct 61 ms 212 KB Output is correct
70 Correct 236 ms 147620 KB Output is correct
71 Correct 174 ms 121292 KB Output is correct
72 Correct 95 ms 75200 KB Output is correct
73 Correct 51 ms 56476 KB Output is correct
74 Correct 144 ms 98888 KB Output is correct
75 Correct 273 ms 170936 KB Output is correct
76 Correct 150 ms 89804 KB Output is correct
77 Correct 68 ms 68988 KB Output is correct
78 Correct 121 ms 99680 KB Output is correct
79 Correct 318 ms 324 KB Output is correct
80 Correct 346 ms 312 KB Output is correct
81 Correct 266 ms 320 KB Output is correct
82 Correct 217 ms 420 KB Output is correct
83 Correct 176 ms 340 KB Output is correct
84 Correct 287 ms 174128 KB Output is correct
85 Correct 286 ms 174232 KB Output is correct
86 Correct 281 ms 174184 KB Output is correct
87 Correct 284 ms 174144 KB Output is correct
88 Correct 288 ms 174140 KB Output is correct
89 Correct 289 ms 174164 KB Output is correct
90 Correct 308 ms 178560 KB Output is correct
91 Correct 312 ms 178512 KB Output is correct
92 Correct 308 ms 178572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 284 ms 173864 KB Output is correct
16 Correct 278 ms 173940 KB Output is correct
17 Correct 278 ms 173864 KB Output is correct
18 Correct 281 ms 174036 KB Output is correct
19 Correct 283 ms 173904 KB Output is correct
20 Correct 300 ms 173824 KB Output is correct
21 Correct 277 ms 173900 KB Output is correct
22 Correct 278 ms 173864 KB Output is correct
23 Correct 283 ms 173936 KB Output is correct
24 Correct 296 ms 178300 KB Output is correct
25 Correct 300 ms 178368 KB Output is correct
26 Correct 305 ms 178380 KB Output is correct
27 Correct 8 ms 852 KB Output is correct
28 Correct 8 ms 876 KB Output is correct
29 Correct 7 ms 880 KB Output is correct
30 Correct 5 ms 884 KB Output is correct
31 Correct 5 ms 828 KB Output is correct
32 Correct 5 ms 852 KB Output is correct
33 Runtime error 1249 ms 524288 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 284 ms 173864 KB Output is correct
16 Correct 278 ms 173940 KB Output is correct
17 Correct 278 ms 173864 KB Output is correct
18 Correct 281 ms 174036 KB Output is correct
19 Correct 283 ms 173904 KB Output is correct
20 Correct 300 ms 173824 KB Output is correct
21 Correct 277 ms 173900 KB Output is correct
22 Correct 278 ms 173864 KB Output is correct
23 Correct 283 ms 173936 KB Output is correct
24 Correct 296 ms 178300 KB Output is correct
25 Correct 300 ms 178368 KB Output is correct
26 Correct 305 ms 178380 KB Output is correct
27 Correct 8 ms 852 KB Output is correct
28 Correct 8 ms 876 KB Output is correct
29 Correct 7 ms 880 KB Output is correct
30 Correct 5 ms 884 KB Output is correct
31 Correct 5 ms 828 KB Output is correct
32 Correct 5 ms 852 KB Output is correct
33 Runtime error 1249 ms 524288 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -