This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;
int nn;
int n, m;
int c[N];
pair<int, int> a[N];
int ans[N];
vector<int> v[N];
int q[N];
set<pair<int, int> > s;
void ubd(int i, int y)
{
if (s.find(m_p(q[a[i].fi], a[i].fi)) != s.end())
s.erase(m_p(q[a[i].fi], a[i].fi));
if (s.find(m_p(q[a[i].se], a[i].se)) != s.end())
s.erase(m_p(q[a[i].se], a[i].se));
q[a[i].fi] += y;
q[a[i].se] += y;
if (a[i].fi)
s.insert(m_p(q[a[i].fi], a[i].fi));
if (a[i].se)
s.insert(m_p(q[a[i].se], a[i].se));
}
void solvv()
{
for (int x = 1; x <= m; ++x)
{
v[x].clear();
q[x] = 0;
}
s.clear();
for (int i = 1; i <= n; ++i)
{
v[a[i].fi].push_back(i);
if (a[i].se != a[i].fi)
v[a[i].se].push_back(i);
}
for (int x = 1; x <= m; ++x)
s.insert(m_p(q[x], x));
for (int i = 1; i <= n; ++i)
ubd(i, 1);
for (int x = 1; x <= m; ++x)
{
int yans = 0;
for (int i = 0; i < sz(v[x]); ++i)
{
int j = v[x][i];
ubd(j, -1);
if (a[j].fi == x)
++yans;
if (a[j].se == x)
++yans;
}
yans += (--s.end())->fi;
ans[x] = min(ans[x], nn - yans);
for (int i = 0; i < sz(v[x]); ++i)
{
int j = v[x][i];
ubd(j, 1);
}
}
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> c[i];
nn = n;
for (int i = 1; i <= m; ++i)
ans[i] = n;
if (n % 2 == 0)
{
for (int i = 1; i <= n; i += 2)
{
a[i / 2 + 1] = m_p(c[i], c[i + 1]);
}
n = n / 2;
solvv();
n *= 2;
a[1] = m_p(c[1], 0);
a[2] = m_p(c[n], 0);
for (int i = 2; i < n; i += 2)
{
a[i / 2 + 2] = m_p(c[i], c[i + 1]);
}
n = 2 + (n - 2) / 2;
solvv();
}
else
{
a[1] = m_p(c[1], 0);
for (int i = 2; i <= n; i += 2)
a[i / 2 + 1] = m_p(c[i], c[i + 1]);
n = (n - 1) / 2 + 1;
solvv();
n = (n - 1) * 2 + 1;
a[1] = m_p(c[n], 0);
for (int i = 1; i < n; i += 2)
a[i / 2 + 2] = m_p(c[i], c[i + 1]);
solvv();
}
for (int i = 1; i <= m; ++i)
cout << ans[i] << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# | 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... |