#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
using namespace std;
typedef int ll;
typedef pair<ll,ll> p;
typedef pair<ll, p> tri;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<p> vp;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<vv> vvv;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef vector<vvvp> vvvvp;
#define double long double
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define loop(a) for (ll i = 0; i < a; i++)
#define setmin(a, b) a = min(a, b)
#define setmax(a, b) a = max(a, b)
#define all(v) v.begin(), v.end()
#include "paint.h"
bool query(ll l, ll r, vv &st)
{
ll n = st.size() / 2;
l += n; r += n;
v cur;
bool first = false;
while (l <= r)
{
if (l % 2 == 1)
{
if (first)
{
v next;
set_intersection(all(cur),all(st[l]),back_inserter(next));
cur = next;
}
else
{
cur = st[l];
first = true;
}
l++;
}
if (r % 2 == 0)
{
if (first)
{
v next;
set_intersection(all(cur),all(st[r]),back_inserter(next));
cur = next;
}
else
{
cur = st[r];
first = true;
}
r--;
}
l /= 2; r /= 2;
}
return cur.size() > 0;
}
int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
vv validStartIndices(N);
vv appearingIn(K);
for (ll i = 0; i < N; i++) appearingIn[C[i]].pb(i);
for (ll i = 0; i < M; i++)
{
for (ll x : B[i])
{
for (ll y : appearingIn[x]) validStartIndices[y].pb((i - y + M * N) % M);
}
}
vb poss(N - M + 1);
v histogram(M, 0);
ll countM = 0;
for (ll i =0; i < M; i++)
{
for (ll x : validStartIndices[i])
{
histogram[x]++;
if (histogram[x] == M) countM++;
}
}
for (ll i = 0; i < N - M + 1; i++)
{
poss[i] = countM;
for (ll x : validStartIndices[i])
{
histogram[x]--;
if (histogram[x] == M - 1) countM--;
}
for (ll x : validStartIndices[i + M])
{
histogram[x]++;
if (histogram[x] == M) countM++;
}
}
ll res = 0;
bool possible = true;
ll last = 0;
ll lastDone = 0;
while (last < N)
{
ll lastFound = -1;
for (;lastDone<=min(last, N - M);lastDone++)
{
if (poss[lastDone]) lastFound = lastDone;
}
if (lastFound == -1)
{
possible = false;
break;
}
last = lastFound + M;
res++;
}
if (!possible) return -1;
return res;
}
/*
int main() {
int N, M, K;
assert(3 == scanf("%d %d %d", &N, &M, &K));
vector<int> C(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &C[i]));
}
vector<int> A(M);
vector<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;
}
/**/
Compilation message
paint.cpp:3: warning: ignoring '#pragma optimization_level ' [-Wunknown-pragmas]
3 | #pragma optimization_level 3
|
paint.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
8 | #pragma GCC optimization ("unroll-loops")
|
paint.cpp:174:1: warning: "/*" within comment [-Wcomment]
174 | /**/
|
paint.cpp:35:16: warning: overflow in conversion from 'double' to 'll' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
35 | const ll INF = 1e18;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |