이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "paint.h"
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int inf = 1001001001;
class RMQ {
int n;
vi val;
public:
RMQ(int _n) {
n = 1;
while (n < _n) n *= 2;
val.assign(n * 2, 0);
}
void update(int i, int x) {
assert(0 <= i and i < n);
i += n;
val[i] += x;
while (i > 1) {
i /= 2;
val[i] = max(val[i * 2], val[i * 2 + 1]);
}
}
int fold(int l, int r) {
assert(0 <= l and l <= r and r <= n);
l += n, r += n;
int fl = 0, fr = 0;
while (l < r) {
if (l & 1) fl = max(fl, val[l++]);
if (r & 1) fr = max(val[--r], fr);
l >>= 1, r >>= 1;
}
return max(fl, fr);
}
};
int minimumInstructions(int n, int m, int k, vi c, vi a, vvi b) {
vvi f(k);
rep(i, m) for (int j : b[i]) f[j].pb(i);
RMQ rmq(m);
auto add = [&](int i) {
assert(0 <= i and i < n);
for (int j : f[c[i]]) rmq.update(((j - i) % m + m) % m, 1);
};
auto erase = [&](int i) {
assert(0 <= i and i < n);
for (int j : f[c[i]]) rmq.update(((j - i) % m + m) % m, -1);
};
rep(i, m - 1) add(i);
vi ok;
rep(i, n - m + 1) {
add(i + m - 1);
if (rmq.fold(0, m) == m) ok.pb(i);
erase(i);
}
int now = -1, ans = 0;
while (now < n - 1) {
auto it = upper_bound(all(ok), now + 1);
if (it == ok.begin()) return -1;
it--;
if (*it + m - 1 <= now) return -1;
now = *it + m - 1;
ans++;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |