跳到正文

题解

P11190 「KDOI-10」Anti-palindrome Solution

MatrixGroup约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月28日
篇幅
约 11 分钟
获赞
24
题目
P11190

版本

  1. v1当前

Solution from tester.

Analysis

Consider an upper bound of kk.

When a majority of ss doesn’t exist

Since a string of length 11 must be a palindrome, kk must be no more than ⌊n2⌋\left\lfloor\dfrac n2\right\rfloor. The following algorithm gives such a construction.

Define a k−k-reduction is to choose any mode of the ss and any other k−1k-1 existing character(s) and make them as a subsequence, then delete them from the string. If nn is even, do 2−2-reduction n2\dfrac n2 times. If nn is odd, do 3−3-reduction once and then do 2−2-reduction n−32\dfrac{n-3}2 times.

One can inductively proves that the algorithm is correct by the fact that after each step, a majority of ss still doesn’t exist. One can implement the algorithm fast enough by maintaining characters by buckets.

When a majority of ss exists

Let the indices of non-majority characters be x1,x2,⋯ ,xℓx_1,x_2,\cdots,x_\ell. Since any string consisting with characters all the same must be a palindrome, kk must be no more than ℓ\ell.

When ℓ=0\ell=0

k≤0k\le 0, which means there is no solution.

When ℓ=1\ell=1

k≤1k\le 1, which means the only solution is to choose the whole string as a substring, if possible. Just check whether x1=n+12x_1=\dfrac{n+1}{2}.

When ℓ≥2\ell \ge 2

The idea is the following: for most non-majority characters, pair them with one majority character each, and add the rest majority characters to some specific subsequences.

When x1=1x_1=1

For every i=2,3,⋯ ,ℓi=2,3,\cdots,\ell, choose any majority character left to pair with. Add the rest majority characters to the subsequence containing sx1s_{x_1}. Since x1=1x_1=1, the first character of the subsequence is sx1s_{x_1}(non-majority), while the last is majority, therefore the construction is valid.

When xℓ=nx_\ell =n

For every i=1,2,⋯ ,ℓ−1i=1,2,\cdots,\ell-1, choose any majority character left to pair with. Add the rest majority characters to the subsequence containing sx1s_{x_1}. Since xℓ=nx_\ell=n, the last character of the subsequence is sxℓs_{x_\ell}(non-majority), while the first is majority, therefore the construction is valid.

When 2≤xi≤n−12\le x_i\le n-1

Let sns_n pair with sx1s_{x_1}, s1s_1 pair with sxℓs_{x_\ell}, and for i=2,3,⋯ ,ℓ−1i=2,3,\cdots,\ell -1, choose any majority character left to pair with. For each majority character sjs_j left, add it to the subsequence containing sx1s_{x_1}, if j>x1j>x_1, otherwise sxℓs_{x_\ell}. Since j≤x1j\le x_1 implies j<xlj<x_l, the last character of the subsequence containing sxℓs_{x_\ell} is xℓx_\ell(non-majority), while the first is majority, and the first character of the subsequence containing sx1s_{x_1} is sx1s_{x_1}(non-majority), while the last is majority, therefore the construction is valid.

Code

C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,del##i##verme=int(n);i<del##i##verme;++i)
#define rep1(i,n) for(int i=1,parano##i##a=int(n);i<=parano##i##a;++i)
#define per(i,n) for(int i=int(n)-1;i>=0;--i)
#define per1(i,n) for(int i=int(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define y0 LingLuo
#define y1 VividCycle
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
unsigned time_related_rand()
{
	return unsigned(std::chrono::steady_clock::now().time_since_epoch().count());
}
int c,t,n;
string s;
vector<int> idx[28];
vector<int> p,q;
int mod;
vector<int> ans[50004];int cnt;
void report()
{
	cout<<"Huoyu\n";
	cout<<cnt<<"\n";
	rep1(i,cnt)
	{
		cout<<ans[i].size();
		sort(ans[i].begin(),ans[i].end());
		for(int x:ans[i])cout<<" "<<x;
		cout<<'\n';
	}
	return ;
}
void Q()
{
	cin>>n>>s;
	rep1(i,26) idx[i].clear();
	rep(i,n) idx[s[i]&31].pb(i+1);
	mod=0;rep1(i,26) if(int(idx[i].size())*2>n) mod=i;
	rep1(i,n/2) ans[i].clear();
	cnt=0;
	if(mod==0)
	{
		if(n&1)
		{
			rep1(_,n/2)
			{
				int cur=1;rep1(i,26) if(idx[i].size()>idx[cur].size()) cur=i;
				int nxt=1;rep1(i,26) if(i!=cur&&!idx[i].empty()){nxt=i;break;}
				++cnt;
				if(_==1)
				{
					int lst=1;rep1(i,26) if(i!=nxt&&i!=cur&&!idx[i].empty()){lst=i;break;}
					ans[cnt].pb(idx[lst].back());idx[lst].pop_back();
				}
				ans[cnt].pb(idx[cur].back());idx[cur].pop_back();
				ans[cnt].pb(idx[nxt].back());idx[nxt].pop_back();
			}
		}
		else
		{
			rep1(_,n/2)
			{
				int cur=1;rep1(i,26) if(idx[i].size()>idx[cur].size()) cur=i;
				int nxt=1;rep1(i,26) if(i!=cur&&!idx[i].empty()){nxt=i;break;}
				++cnt;
				ans[cnt].pb(idx[cur].back());idx[cur].pop_back();
				ans[cnt].pb(idx[nxt].back());idx[nxt].pop_back();
			}
		}
		report();
		return ;
	}
	p.clear();q.clear();
	rep1(i,26) idx[i].clear();
	rep(i,n) if((s[i]&31)==mod) p.pb(i+1); else q.pb(i+1);
	if(q.empty())
	{
		cout<<"Shuiniao\n";return ;
	}
	if(int(q.size())==1)
	{
		if(q[0]*2-1==n)
		{
			cout<<"Shuiniao\n";return ;
		}
		cnt=1;
		rep1(i,n) ans[1].pb(i);
		report();
		return ;
	}
	if(q[0]==1)
	{
		for(int x:q) if(x!=1)
		{
			ans[++cnt].pb(x);
			ans[cnt].pb(p.back());p.pop_back();
		}
		ans[++cnt].pb(1);
		for(int x:p) ans[cnt].pb(x);
		report();
		return ;
	}
	if(q.back()==n)
	{
		for(int x:q) if(x!=n)
		{
			ans[++cnt].pb(x);
			ans[cnt].pb(p.back());p.pop_back();
		}
		ans[++cnt].pb(n);
		for(int x:p) ans[cnt].pb(x);
		report();
		return ;
	}
	int a=q.back();q.pop_back();
	int b=q.back();q.pop_back();
	ans[++cnt].pb(a);
	ans[++cnt].pb(b);ans[cnt].pb(n);
	p.pop_back();
	for(int x:q)
	{
		ans[++cnt].pb(x);ans[cnt].pb(p.back());p.pop_back();
	}
	for(int x:p)if(x<a) ans[1].pb(x); else ans[2].pb(x);
	report();
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin>>c>>t;while(t--)Q();return 0;
}
原文