답안 #926569

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
926569 2024-02-13T10:17:48 Z green_gold_dog 벽 칠하기 (APIO20_paint) C++17
28 / 100
1500 ms 21588 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
#include "paint.h"

using namespace std;

typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

ll add(ll x, ll m) {
        x++;
        if (x == m) {
                x = 0;
        }
        return x;
}

ll sub(ll x, ll m) {
        x--;
        if (x == -1) {
                x = m - 1;
        }
        return x;
}

ll minimumInstructions(ll n, ll m, ll k, vector<ll> c, vector<ll> a, vector<vector<ll>> b) {
        vector<unordered_set<ll>> all(k);
        for (ll i = 0; i < m; i++) {
                for (auto j : b[i]) {
                        all[j].insert(i);
                }
        }
        vector<ll> col(m);
        for (ll i = 0; i < n; i++) {
                for (auto j : all[c[i]]) {
                        col[j]++;
                }
        }
        ll sum = col[0] + col.back(), now = 0, lst = m - 1;
        for (ll i = 1; i < m; i++) {
                if (assign_min(sum, col[i] + col[i - 1])) {
                        now = i;
                        lst = i - 1;
                }
        }
        vector<ll> cnow(n, 0), clst(n, 0);
        for (ll i = 0; i < n; i++) {
                ll nn = i, nx = now;
                while (nn < n) {
                        if (all[c[nn]].find(nx) == all[c[nn]].end()) {
                                break;
                        }
                        cnow[i]++;
                        nx = add(nx, m);
                        nn++;
                }
                nn = i;
                nx = lst;
                while (nn >= 0) {
                        if (all[c[nn]].find(nx) == all[c[nn]].end()) {
                                break;
                        }
                        clst[i]++;
                        nx = sub(nx, m);
                        nn--;
                }
        }
        vector<bool> cand(n, false);
        deque<ll> ta, td;
        for (ll i = 0; i < n; i++) {
                ll sum = cnow[i];
                ll st = i;
                if (i != 0) {
                        sum += clst[i - 1];
                        st -= clst[i - 1];
                }
                if (sum >= m) {
                        ta.push_back(st);
                        td.push_back(st + sum - m);
                }
        }
        ll ns = 0;
        sort(ta.begin(), ta.end());
        sort(td.begin(), td.end());
        for (ll i = 0; i < n; i++) {
                while (!ta.empty() && ta.front() == i) {
                        ns++;
                        ta.pop_front();
                }
                cand[i] = ns > 0;
                while (!td.empty() && td.front() == i) {
                        ns--;
                        td.pop_front();
                }
        }
        lst = 0;
        ll can = 0;
        ll ans = 0;
        while (can < n) {
                ll ncan = can;
                for (ll j = lst; j <= can; j++) {
                        if (cand[j]) {
                                assign_max(ncan, j + m);
                        }
                }
                lst = can + 1;
                if (ncan == can) {
                        return -1;
                }
                ans++;
                can = ncan;
        }
        return ans;
}

#ifdef LOCAL
int main() {
  int N, M, K;
  assert(3 == scanf("%d %d %d", &N, &M, &K));

  std::vector<int> C(N);
  for (int i = 0; i < N; ++i) {
    assert(1 == scanf("%d", &C[i]));
  }

  std::vector<int> A(M);
  std::vector<std::vector<int>> B(M);
  for (int i = 0; i < M; ++i) {
    assert(1 == scanf("%d", &A[i]));
    B[i].resize(A[i]);
    for (int j = 0; j < A[i]; ++j) {
      assert(1 == scanf("%d", &B[i][j]));
    }
  }

  int minimum_instructions = minimumInstructions(N, M, K, C, A, B);
  printf("%d\n", minimum_instructions);

  return 0;
}
#endif

Compilation message

paint.cpp:13:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
   13 | constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 33 ms 21408 KB Output is correct
23 Correct 40 ms 21196 KB Output is correct
24 Correct 33 ms 21336 KB Output is correct
25 Execution timed out 1525 ms 600 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 3928 KB Output is correct
35 Correct 1 ms 3416 KB Output is correct
36 Correct 2 ms 5724 KB Output is correct
37 Correct 2 ms 4700 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 3 ms 4552 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 3 ms 5724 KB Output is correct
43 Correct 3 ms 5332 KB Output is correct
44 Correct 2 ms 348 KB Output is correct
45 Correct 2 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 2 ms 5980 KB Output is correct
51 Correct 2 ms 5824 KB Output is correct
52 Correct 3 ms 5976 KB Output is correct
53 Correct 2 ms 5980 KB Output is correct
54 Correct 3 ms 5980 KB Output is correct
55 Correct 2 ms 5980 KB Output is correct
56 Correct 2 ms 5980 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 2 ms 5980 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 3 ms 5980 KB Output is correct
61 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 33 ms 21408 KB Output is correct
25 Correct 40 ms 21196 KB Output is correct
26 Correct 33 ms 21336 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 3928 KB Output is correct
38 Correct 1 ms 3416 KB Output is correct
39 Correct 2 ms 5724 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 3 ms 4552 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 3 ms 5724 KB Output is correct
46 Correct 3 ms 5332 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 2 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 2 ms 5980 KB Output is correct
54 Correct 2 ms 5824 KB Output is correct
55 Correct 3 ms 5976 KB Output is correct
56 Correct 2 ms 5980 KB Output is correct
57 Correct 3 ms 5980 KB Output is correct
58 Correct 2 ms 5980 KB Output is correct
59 Correct 2 ms 5980 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 2 ms 5980 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 3 ms 5980 KB Output is correct
64 Correct 1 ms 344 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 1 ms 464 KB Output is correct
67 Correct 2 ms 348 KB Output is correct
68 Correct 2 ms 356 KB Output is correct
69 Correct 2 ms 356 KB Output is correct
70 Correct 4 ms 5476 KB Output is correct
71 Correct 4 ms 4708 KB Output is correct
72 Correct 3 ms 4188 KB Output is correct
73 Correct 4 ms 4560 KB Output is correct
74 Correct 5 ms 5472 KB Output is correct
75 Correct 3 ms 6244 KB Output is correct
76 Correct 27 ms 15384 KB Output is correct
77 Correct 25 ms 18012 KB Output is correct
78 Correct 46 ms 20288 KB Output is correct
79 Correct 2 ms 348 KB Output is correct
80 Correct 2 ms 348 KB Output is correct
81 Correct 3 ms 352 KB Output is correct
82 Correct 2 ms 348 KB Output is correct
83 Correct 2 ms 444 KB Output is correct
84 Correct 7 ms 6744 KB Output is correct
85 Correct 5 ms 6488 KB Output is correct
86 Correct 5 ms 6488 KB Output is correct
87 Correct 5 ms 6488 KB Output is correct
88 Correct 5 ms 6492 KB Output is correct
89 Correct 5 ms 6492 KB Output is correct
90 Correct 46 ms 21408 KB Output is correct
91 Correct 45 ms 21584 KB Output is correct
92 Correct 63 ms 21588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 33 ms 21408 KB Output is correct
25 Correct 40 ms 21196 KB Output is correct
26 Correct 33 ms 21336 KB Output is correct
27 Execution timed out 1525 ms 600 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 33 ms 21408 KB Output is correct
25 Correct 40 ms 21196 KB Output is correct
26 Correct 33 ms 21336 KB Output is correct
27 Execution timed out 1525 ms 600 KB Time limit exceeded
28 Halted 0 ms 0 KB -