P11190 「KDOI-10」Anti-palindrome Solution

· · 题解

Solution from tester.

Analysis

Consider an upper bound of k.

When a majority of s doesn't exist

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

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

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

When a majority of s exists

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

When \ell=0

#### When $\ell=1 #### When $\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 x_1=1

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

When x_\ell =n

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

When 2\le x_i\le n-1

Let s_n pair with s_{x_1}, s_1 pair with s_{x_\ell}, and for i=2,3,\cdots,\ell -1, choose any majority character left to pair with. For each majority character s_j left, add it to the subsequence containing s_{x_1}, if j>x_1, otherwise s_{x_\ell}. Since j\le x_1 implies j<x_l, the last character of the subsequence containing s_{x_\ell} is x_\ell(non-majority), while the first is majority, and the first character of the subsequence containing s_{x_1} is s_{x_1}(non-majority), while the last is majority, therefore the construction is valid.

Code

#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;
}