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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
set<string> S;
const int N = 20;
void gen()
{
queue<string> q;
q.push("10");S.insert("10");
q.push("11");S.insert("11");
q.push("00");S.insert("00");
while(!q.empty())
{
string s=q.front(); q.pop();
for(int i=0;i<s.length();i++)
{
string t=s.substr(0,i+1);
reverse(t.begin(),t.end());
t+=s;
if(t.length()<=N&&S.find(t)==S.end())
{
S.insert(t); q.push(t);
}
}
reverse(s.begin(),s.end());
if(s.length()<=N&&S.find(s)==S.end())
{
S.insert(s); q.push(s);
}
}
}
int a[1111111];
int ans[1111111];
vector<vi> dp;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//gen();
//int n; cin>>n;
/*
for(auto s:S)
{
if(int(s.length())<=n)
{
cout<<s<<'\n';
}
}
*/
/*
vector<string> L,R;
for(int i=0;i<(1<<n);i++)
{
string s;
for(int j=n-1;j>=0;j--)
{
if(i&(1<<j)) s+='1';
else s+='0';
}
if(S.find(s)!=S.end())
{
R.pb(s);
}
else
{
L.pb(s);
}
}
cerr<<"ACCEPTED\n";
for(auto s:R)
{
for(int j=0;j<s.length();j+=2)
{
int cur=s[j]-'0'; int nxt=s[j+1]-'0';
cerr<<cur*2+nxt;
}
cerr<<'\n';
}
cerr<<'\n';
*/
int n,m; cin>>n>>m;
for(int i=0;i<m;i++) ans[i]=int(1e9);
for(int i=0;i<n;i++)
{
cin>>a[i]; a[i]--;
}
for(int i=0;i<m;i++)
{
for(int j=i;j<m;j++)
{
//all 0 or 3
int mx=0;
if(n&1) mx+=(a[0]==i||a[0]==j);
for(int k=(n&1);k<n;k+=2)
{
mx+=max((a[k]==i)+(a[k+1]==i),(a[k]==j)+(a[k+1]==j));
}
//special pattern
{
int as = 0;
dp.resize(n);
for(int i=0;i<n;i++) dp[i].assign(4,-int(1e9));
if(n&1)
{
dp[0][0] = (a[0]==i);
dp[0][1] = (a[0]==j);
}
for(int k=(n&1);k<n;k+=2)
{
for(int l=0;l<4;l++)
{
if(k==0)
{
dp[k][l]=0;
}
else
{
if(l==0)
{
dp[k][l]=max(dp[max(k-2,0)][0], dp[max(k-2,0)][2]);
}
else if(l==1)
{
dp[k][l]=max(dp[max(k-2,0)][0], dp[max(k-2,0)][2]);
}
else if(l==2)
{
dp[k][l]=max(dp[max(k-2,0)][1], dp[max(k-2,0)][3]);
}
else
{
dp[k][l]=max(dp[max(k-2,0)][3],dp[max(k-2,0)][1]);
}
}
dp[k][l]+=(l&1?a[k+1]==j:a[k+1]==i);
dp[k][l]+=(l/2?a[k]==j:a[k]==i);
}
}
for(int i=0;i<4;i++) as=max(as,dp[n-2][i]);
mx=max(mx,as);
}
ans[i]=min(ans[i],n-mx); ans[j]=min(ans[j],n-mx);
}
}
for(int i=0;i<m;i++)
{
cout<<ans[i]<<'\n';
}
}
Compilation message (stderr)
rope.cpp: In function 'void gen()':
rope.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<s.length();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... |