跳到正文

题解

Codeforces Round 1014 (Div. 2) D

__Potata__约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年4月10日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 6 分钟
获赞
2
题目
CF2092D

版本

  1. v1当前

CF2092D Mishkin Energizer

解析

显然在注意到 nn 的数据范围时,
我们就可以考虑本题的大模拟解法了。

首先,对于每个选定的操作数 ii,添加的 xx 是固定的,即:

x=L xor⁡ I xor⁡ T xor⁡ Si xor⁡ Si+1x = L\ \operatorname{xor}\ I\ \operatorname{xor}\ T\ \operatorname{xor}\ S_i\ \operatorname{xor}\ S_{i + 1}

这仅是一个计算方法,其正确性我们不难证明。


证明:

∵ x≠Si; x≠Si+1; x∈{L, I, T}∵\ x \neq S_i;\ x \neq S_{i + 1};\ x \in \{\texttt{L},\ \texttt{I},\ \texttt{T}\} ∴ x∈{L, I, T} −{Si, Si+1}∴\ x \in \{\texttt{L},\ \texttt{I},\ \texttt{T}\}\ - \{S_i,\ S_{i + 1}\}

又因为 Si≠Si+1S_i \neq S_{i + 1} 且 S∈{L, I, T}S \in \{\texttt{L},\ \texttt{I},\ \texttt{T}\}。

故 {{L, I, T} −{Si, Si+1}}\{\{\texttt{L},\ \texttt{I},\ \texttt{T}\}\ - \{S_i,\ S_{i + 1}\}\} 中有且仅有 11 个元素。

证得:xx 的取值是唯一的,且显而易见地可以通过上式计算出来。


于是对于每次操作(最坏情况下):

我们使用 O(n)O(n) 的时间复杂度遍历字符串。

O(1)O(1) 决策每个字符,并在 SiS_i 与 Si+1S_{i + 1} 对应的 xx 较少时选择添加,否则决策下一个。

每次添加的时间复杂度为 O(n)O(n)。

共 2n2n 次操作。故时间复杂度为 O(n3)O(n^3)。


代码

码风清奇、注释详尽。

C++
#include<iostream>
#include<vector>
#include<string>
#include<map>

#define potata 105
#define cal ('L' ^ 'I' ^ 'T')
using namespace std;

int q;
string str;
int n;
map<char, int> cnt;
vector<int> ans;

bool equal() 
{ 
    return (cnt['L'] == cnt['I'] && cnt['L'] == cnt['T'] && cnt['I'] == cnt['T']);
} // 判断饮料平衡

void solve() 
{
    cin >> n;
    cin >> str;

    cnt.clear();
    ans.clear();
    // 多测不清见祖宗

    for (int i = 0; i < n; i++)
        cnt[str[i]]++;
    
    int step = 0;
    while (equal() == false && step <= 2 * n) // O(2n) 次操作
    {
        bool flg = false;

        for (int i = 0; i < str.size() - 1; i++) // O(n) 遍历
        {
            if (str[i] == str[i + 1]) 
                continue; // 可行性

            char ch = cal ^ str[i] ^ str[i + 1];
            int Cnt = cnt[ch];
            int Max = max(cnt['L'], max(cnt['I'], cnt['T']));
            // 最优性

            if (Cnt < Max)
            {
                str.insert(str.begin() + i + 1, ch); // O(n) 插入
                ans.push_back(i + 1);

                cnt[ch]++;
                flg = true;
                break;
            } // 小优化
        }

        if (flg == false) 
        {
            for (int i = 0; i < str.size() - 1; i++) 
            {
                if (str[i] != str[i + 1]) 
                {
                    char ch = cal ^ str[i] ^ str[i + 1];

                    str.insert(str.begin() + i + 1, ch);
                    ans.push_back(i + 1);

                    cnt[ch]++;
                    flg = true;
                    break;
                }
            }
        } // 确保最优性

        if (flg == false) 
            break; // 可行性(剪枝)
        
        step++;
    } // 上面的步骤是至多 2n 次操作的最优方案

    if (equal()) 
    {
        int Ans = ans.size();
        
        cout << Ans << '\n';
        for (int i = 0; i < Ans; i++)
            cout << ans[i] << '\n';
    }

    else
        cout << -1 << '\n';
}

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie();

    cin >> q;

    while (q--)
        solve();
    
    return 0;
}
原文
存档评论1 条抓取于