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>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const LL llinf=9000000000000000000;
const int inf=2000000000;
const LL hashnum=100000000;
int n, m;
int arr[1000010];
int cnt[1000010], cnt2[1000010];
vector<pii> q;
struct SEGMENT_TREE
{
int initial=-inf;
int x;
struct NODE{
int st, fin;
int q;
}tree[4000000];
int f(int a, int b){return max(a, b);}
void init(int point, int num){
if(num==1)tree[point].st=tree[point].fin=++x;
if(num<=1)return;
init(point*2, num-num/2);
init(point*2+1, num/2);
tree[point].st=tree[point*2].st;
tree[point].fin=tree[point*2+1].fin;
}
void cl(int point){
tree[point].q=0;
if(tree[point].st==tree[point].fin)return;
cl(point*2);
cl(point*2+1);
}
void update(int point, int num, int qu){
if(tree[point].st==tree[point].fin){
tree[point].q+=qu;
return;
}
if(num<=tree[point*2].fin)update(point*2, num, qu);
else update(point*2+1, num, qu);
tree[point].q=f(tree[point*2].q, tree[point*2+1].q);
}
int query(int point, int a, int b){
if(tree[point].st>=a&&tree[point].fin<=b)return tree[point].q;
if(tree[point].st>b||tree[point].fin<a)return initial;
return f(query(point*2, a, b), query(point*2+1, a, b));
}
void init(int num){init(1, num);}
void update(int num, LL qu){update(1, num, qu);}
void cl(){cl(1);}
int query(int a, int b){return query(1, a, b);}
}seg;
int ans[1000010];
int main()
{
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%d", &arr[i]);
if(i%2==0){
if(arr[i-1]!=arr[i]){
q.pb(mp(arr[i], arr[i-1]));
q.pb(mp(arr[i-1], arr[i]));
cnt[arr[i]]++;
cnt[arr[i-1]]++;
}
else{
cnt2[arr[i]]+=2;
}
}
if(n%2&&i==n){
cnt2[arr[i]]++;
}
}
sort(all(q));
seg.init(m);
for(int i=1; i<=m; i++){
seg.update(i, cnt[i]+cnt2[i]);
ans[i]=inf;
}
int pv=0;
for(int i=1; i<=m; i++){
int temppv=pv;
seg.update(i, -cnt[i]-cnt2[i]);
for(; pv<q.size(); pv++){
if(q[pv].F!=i)break;
seg.update(q[pv].S, -1);
}
int turna=cnt[i];
int turnb=n-cnt[i]*2-cnt2[i]-seg.query(1, m);
ans[i]=min(ans[i], turna+turnb);
seg.update(i, cnt[i]+cnt2[i]);
for(; temppv<pv; temppv++){
seg.update(q[temppv].S, 1);
}
}
seg.cl();
memset(cnt, 0, sizeof(cnt));
memset(cnt2, 0, sizeof(cnt2));
q.clear();
for(int i=1; i<=n; i++){
if(i==1){
cnt2[arr[i]]++;
}
else if(i%2){
if(arr[i-1]!=arr[i]){
q.pb(mp(arr[i], arr[i-1]));
q.pb(mp(arr[i-1], arr[i]));
cnt[arr[i]]++;
cnt[arr[i-1]]++;
}
else{
cnt2[arr[i]]+=2;
}
}
if(n%2==0&&i==n){
cnt2[arr[i]]++;
}
}
sort(all(q));
for(int i=1; i<=m; i++)seg.update(i, cnt[i]+cnt2[i]);
pv=0;
for(int i=1; i<=m; i++){
int temppv=pv;
seg.update(i, -cnt[i]-cnt2[i]);
for(; pv<q.size(); pv++){
if(q[pv].F!=i)break;
seg.update(q[pv].S, -1);
}
int turna=cnt[i];
int turnb=n-cnt[i]*2-cnt2[i]-seg.query(1, m);
ans[i]=min(ans[i], turna+turnb);
seg.update(i, cnt[i]+cnt2[i]);
for(; temppv<pv; temppv++){
seg.update(q[temppv].S, 1);
}
}
for(int i=1; i<=m; i++){
printf("%d\n", ans[i]);
}
}
Compilation message (stderr)
rope.cpp: In function 'int main()':
rope.cpp:91:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; pv<q.size(); pv++){
~~^~~~~~~~~
rope.cpp:132:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; pv<q.size(); pv++){
~~^~~~~~~~~
rope.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
rope.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[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... |