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 pb push_back
const int N=1000050;
const int M=2*N;
struct SegmentTree
{
int mx[M];
SegmentTree(){}
void Set(int i, int f){ mx[i+=N]+=f;for(i>>=1;i;i>>=1) mx[i]=max(mx[i<<1],mx[i<<1|1]);}
int Get(int l, int r)
{
int ans=0;
for(l+=N,r+=N;l<=r;l>>=1,r>>=1)
{
if(l%2==1) ans=max(ans,mx[l++]);
if(r%2==0) ans=max(ans,mx[r--]);
}
return ans;
}
} ST[2];
int a[N];
vector<int> pos[N];
int main()
{
int n,k;
scanf("%i %i",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%i",&a[i]);
ST[0].Set(a[i],1);
ST[1].Set(a[i],1);
pos[a[i]].pb(i);
}
for(int i=1;i<=k;i++)
{
vector<int> del[2];
for(int x:pos[i])
{
del[0].pb(x);
del[1].pb(x);
int y;
if(x%2==1) y=x+1;
else y=x-1;
if(y>0 && y<=n) del[0].pb(y);
if(x%2==0) y=x+1;
else y=x-1;
if(y>0 && y<=n) del[1].pb(y);
}
sort(del[0].begin(),del[0].end());
del[0].erase(unique(del[0].begin(),del[0].end()),del[0].end());
sort(del[1].begin(),del[1].end());
del[1].erase(unique(del[1].begin(),del[1].end()),del[1].end());
for(int x:del[0]) ST[0].Set(a[x],-1);
for(int x:del[1]) ST[1].Set(a[x],-1);
int ans=n-pos[i].size()-max(ST[0].Get(1,k),ST[1].Get(1,k));
printf("%i\n",ans);
for(int x:del[0]) ST[0].Set(a[x],1);
for(int x:del[1]) ST[1].Set(a[x],1);
}
return 0;
}
Compilation message (stderr)
rope.cpp: In function 'int main()':
rope.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&k);
~~~~~^~~~~~~~~~~~~~~
rope.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&a[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... |