#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto a : (b))
#define repIn2(a, b, c) for(auto [a, b] : (c))
constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl
#define ll long long
#define pb push_back
#define int ll
using namespace std;
struct hsh {
ll operator() (pair<int, int> x) const {
ll a = x.first, b = x.second;
ll ret = (a << 32) | b;
return ret ^ 213769420213742069;
}
};
constexpr int maxm = 1e6 + 7, maxTb = 1 << 20;
int n, m;
int iniCol[maxm];
int ans[maxm];
unordered_map<pair<int, int>, int, hsh> ileTakich;
//map<pair<int, int>, int> ileTakich;
int ilePojedynczych[maxm], ilePodwojnych[maxm];
unordered_map<pair<int, int>, int, hsh> ileTakich2;
int ilePojedynczych2[maxm], ilePodwojnych2[maxm];
vector<int> skim[maxm];
pair<int, int> t[2 * maxTb];
int tb;
void checkCols(int c1, int c2) {
if(c1 > c2) swap(c1, c2);
int ileWsumie = n / 2;
auto ilePoj = ilePojedynczych[c1] + (c1 == c2 ? 0 : ilePojedynczych[c2] - (ileTakich.contains(pair{c1, c2}) ? ileTakich[pair{c1, c2}] : 0));
auto ilePod = ilePodwojnych[c1] + (c1 == c2 ? 0 : ilePodwojnych[c2]);
auto cost1 = (ileWsumie - ilePod) * 2 - ilePoj;
if(n % 2 == 1) cost1 += iniCol[n - 1] != c1 && iniCol[n - 1] != c2;
if(n % 2 == 0) ileWsumie--;
ilePoj = ilePojedynczych2[c1] + (c1 == c2 ? 0 : ilePojedynczych2[c2] - (ileTakich2.contains(pair{c1, c2}) ? ileTakich2[pair{c1, c2}] : 0));
ilePod = ilePodwojnych2[c1] + (c1 == c2 ? 0 : ilePodwojnych2[c2]);
auto cost2 = (ileWsumie - ilePod) * 2 - ilePoj;
cost2 += iniCol[0] != c1 && iniCol[0] != c2;
if(n % 2 == 0) cost2 += iniCol[n - 1] != c1 && iniCol[n - 1] != c2;
auto cost = min(cost1, cost2);
ans[c1] = min(ans[c1], cost);
ans[c2] = min(ans[c2], cost);
}
void tSet(int i, int x) {
i += tb;
t[i].first = x;
while(i > 1) i /= 2, t[i] = max(t[2 * i], t[2 * i + 1]);
}
pair<int, int> tQ(int l, int r) {
l += tb;
r += tb;
auto ret = max(t[l], t[r]);
while(l / 2 != r / 2) {
if(l % 2 == 0) ret = max(ret, t[l + 1]);
if(r % 2 == 1) ret = max(ret, t[r - 1]);
l /= 2; r /= 2;
}
return ret;
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
scanf("%lld%lld", &n, &m);
rep(i, 0, n) scanf("%lld", iniCol + i);
tb = 1 << (64 - __builtin_clzll(max(1ll, m - 1)));
rep(i, 1, m + 1) ans[i] = 1e9;
rep(i, 0, n - 1) {
auto c1 = iniCol[i], c2 = iniCol[i + 1];
if(i % 2 == 0) {
if(c1 == c2) ilePodwojnych[c1]++;
else {
if(!ileTakich[pair{min(c1, c2), max(c1, c2)}] && !ileTakich2[pair{min(c1, c2), max(c1, c2)}]) skim[c1].pb(c2), skim[c2].pb(c1);
ilePojedynczych[c1]++, ilePojedynczych[c2]++, ileTakich[pair{min(c1, c2), max(c1, c2)}]++;
}
}
else {
if(c1 == c2) ilePodwojnych2[c1]++;
else {
if(!ileTakich[pair{min(c1, c2), max(c1, c2)}] && !ileTakich2[pair{min(c1, c2), max(c1, c2)}]) skim[c1].pb(c2), skim[c2].pb(c1);
ilePojedynczych2[c1]++, ilePojedynczych2[c2]++, ileTakich2[pair{min(c1, c2), max(c1, c2)}]++;
}
}
}
rep(i, 0, n - 1) checkCols(iniCol[i], iniCol[i + 1]);
rep(i, 1, m + 1) checkCols(i, i);
rep(i, 0, tb) t[i + tb] = {-1, i};
rep(i, 1, m + 1) t[i - 1 + tb].first = ilePodwojnych[i] * 2 + ilePojedynczych[i];
repD(i, tb - 1, 0) t[i] = max(t[2 * i], t[2 * i + 1]);
rep(i, 1, m + 1) {
pair<int, int> maxx = {-1, -1};
skim[i].pb(i);
ranges::sort(skim[i]);
auto prv = 1ll;
repIn(j, skim[i]) {
if(j > prv) maxx = max(maxx, tQ(prv - 1, j - 2));
prv = j + 1;
}
if(prv <= m) maxx = max(maxx, tQ(prv - 1, m - 1));
checkCols(i, maxx.second + 1);
}
rep(i, 0, tb) t[i + tb] = {-1, i};
rep(i, 1, m + 1) t[i - 1 + tb].first = ilePodwojnych2[i] * 2 + ilePojedynczych2[i];
repD(i, tb - 1, 0) t[i] = max(t[2 * i], t[2 * i + 1]);
rep(i, 1, m + 1) {
pair<int, int> maxx = {-1, -1};
auto prv = 1ll;
repIn(j, skim[i]) {
if(j > prv) maxx = max(maxx, tQ(prv - 1, j - 2));
prv = j + 1;
}
if(prv <= m) maxx = max(maxx, tQ(prv - 1, m - 1));
checkCols(i, maxx.second + 1);
}
rep(i, 0, m) printf("%lld\n", ans[i + 1]);
}
Compilation message (stderr)
rope.cpp: In function 'int32_t main()':
rope.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%lld%lld", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
rope.cpp:82:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | rep(i, 0, n) scanf("%lld", iniCol + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |