题解:P13361 [GCJ 2011 Qualification]

· · 题解

题目大意

题目传送门
就是计算两个机器人按照给定顺序按下按钮序列所需的最短时间。

题目分析

我们只需要模拟两个机器人(蓝色和橙色)的当前位置以及它们上一次操作完成的时间。对于每个按钮,计算当前机器人完成该操作所需的时间,即该机器人上一个操作完成的时间加上移动时间(当前位置到目标位置的差的绝对值加 1 秒的按键时间)。最后,总时间更新为当前操作完成的时间与另一个机器人完成上一个操作的时间中的较大值即可。

代码实现

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define int long long

#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int ti;

void f(int t){
    int x;
    cin>>x;
    vector<pair<char,int> >a(x);//颜色和位置
    for(int i=0;i<x;i++){
        cin>>a[i].first>>a[i].second;
    }
    int ans=0;
    int poso=1,posb=1;//初始位置
    int tb=0,to=0;//初始时间
    for(auto &e:a){
        if(e.first=='O'){
            int mt=abs(e.second-poso)+1;
            to=max(to+mt,tb+1);
            poso=e.second;
            ans=max(ans,to);
        }
        else{
            int mt=abs(e.second-posb)+1;
            tb=max(tb+mt,to+1);
            posb=e.second;
            ans=max(ans,tb);
        }
    }
    cout<<"Case #"<<t<<": "<<ans<<endl;
}

signed main()
{
    fst
    cin>>ti;
    for(int i=1;i<=ti;i++){
        f(i);
    }
    return 0;
}