[USACO24FEB] Test Tubes S 题解

· · 题解

赛时写出正解没交,写发题解纪念下/kk

在下文中,为了使文章更简短,将某个试管内元素的个数记为试管的「大小」;将某个试管的顶部元素记为试管的「顶」;将烧杯记为「试管 3」。

首先,考虑稍微将题目转换一下:

进行了以上的转换后,就可以进行简单模拟了。

我们使用以下策略进行「倾倒」:

正确性:

对于不需要对试管 3 进行操作的情况,由于按照以上操作一定每一步会使试管 1 的大小与试管 2 的大小之和减 1。很明显不会存在更优的方法,因为每一步至多使以上值减 1。

对于需要对试管 3 进行操作的情况,则需要多两步来对试管 3 进行由空转换至一个元素和由一个元素转换为空。对于试管 1 的底部与试管 2 的底部相同的情况,还需要一步填充合并后的空位。这与上述步骤相符。

别忘了判断一步就可以结束的情况。

Code:

// Problem: Problem 2. Test Tubes
// LuoguUID: 514700 
// LuoguUserName: MicroSun
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fst first
#define scd second
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,s,e) for(int i=s;i>=e;i--)

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;

const int maxn=3e5+10;

pii ans[maxn];
int cnt;
void solve(){
    cnt=0;
    int n,t;
    cin>>n>>t;
    stack<int> s1,s2;
    int e1,e2;
    rep(i,1,n){
        char ch;
        cin>>ch;
        if(s1.empty()) e1=ch-'0';
        if(s1.empty()||ch-'0'!=s1.top())
            s1.push(ch-'0');
    }
    rep(i,1,n){
        char ch;
        cin>>ch;
        if(s2.empty()) e2=ch-'0';
        if(s2.empty()||ch-'0'!=s2.top())
            s2.push(ch-'0');
    }
    int s3=0;
    int lilun=s1.size()+s2.size()-1+(e1==e2);
    //lilun为理论最小值
    bool use_s3=0;
    while(1){
        if(s1.empty()&&s2.size()==2){
            s1.push(s2.top());
            s2.pop();
            ans[++cnt]={2,1};
        }
        else if(s2.empty()&&s1.size()==2){
            s2.push(s1.top());
            s1.pop();
            ans[++cnt]={1,2};
        }
        if(s1.empty()&&s2.size()==1){
            ans[++cnt]={3,1};
            break;
        }
        if(s2.empty()&&s1.size()==1){
            ans[++cnt]={3,2};
            break;
        }
        if(s1.size()==1&&s2.size()==1&&s1.top()!=s2.top()){
            if(!s3) break;
            else{
                if(s1.top()==s3){
                    ans[++cnt]={3,1};
                }
                else{
                    ans[++cnt]={3,2};
                }
                break;
            }
        }
        if(s1.top()==s2.top()){
            if(s1.size()>s2.size()){
                s1.pop();
                ans[++cnt]={1,2};
            }
            else{
                s2.pop();
                ans[++cnt]={2,1};
            }
        }
        else if(!s3){
            use_s3=1;
            if(s1.size()>s2.size()){
                s3=s1.top();
                s1.pop();
                ans[++cnt]={1,3};
            }
            else{
                s3=s2.top();
                s2.pop();
                ans[++cnt]={2,3};
            }
        }
        else{
            if(s1.top()==s3){
                s1.pop();
                ans[++cnt]={1,3};
            }
            else{
                s2.pop();
                ans[++cnt]={2,3};
            }
        }       
    }
    if(!use_s3) --lilun;
    //此时cnt一定等于lilun
    cout<<lilun<<endl;
    if(t!=1){
        rep(i,1,cnt){
            cout<<ans[i].fst<<' '<<ans[i].scd<<endl;
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin>>t;
    while(t--) solve();
    return 0;
}