CodeForces 74 B.Train(贪心)

Description

一列火车有 n 节车厢,从车头到车尾编号为 1 ~ n ,起初逃票的人和查票的人在两节不同的车票,查票的人有一个查票方向(朝着车头或朝着车尾),查到一段就会转向继续查票,当列车在行驶时,逃票的要么移动到相邻车厢要么在原地不动,当列车停下时,逃票的可以下车,如果此时是终点站则逃票成功,否则他会选择任一节车厢上车。每一分钟两人会移动一次,如果是在行驶过程中则逃票先移动,如果是已经停车则查票的先移动然后逃票的再上车。如果某个时刻查票的和逃票的在一个车厢则查票的成功,逃票的知道所有信息,如果有不被抓到的方案则他逃票成功,如果无论如何都会被抓则他会尽量拖延时间,此时求在采取最佳策略下他被抓的时间

Input

第一行三个整数 n , S , C 表示车厢数量以及初始状态逃票人和查票人的位置,之后输入查票人查票的方向,最后输入一个串长不超过 200 01 串表示每个时刻列车的状态, 1 表示停车 0 表示正在行驶 ( 2 n 50 )

Output

若逃票成功则输出 S t o w a w a y ,否则输出 C o n t r o l l e r 并输出被抓的最晚时间

Sample Input

5 3 2
to head
0001001

Sample Output

Stowaway

Solution

贪心,行驶过程中逃票的会向离查票的更远的位置移动,如果不能增大两者之间的距离则原地不动,在停车的时候,若查票的在车厢一段,则逃票的从车厢另一端上车,否则逃票的在查票的后一节车厢,跟着查票的走可以尽可能拖延时间

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=205;
int n,S,C,dx;
char c[maxn],s[maxn];
void Solve()
{
    if(S==C)
    {
        printf("Controller 0\n");
        return ;
    }
    S--,C--;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(s[i]=='1')
        {
            C=(C+dx+n)%n;
            if(C==0||C==n-1)dx=-dx;
            if(C==0)S=n-1;
            else if(C==n-1)S=0;
            else S=C-dx;
        }
        else
        {
            if(C<=S)S=(S==n-1?S:S+1);
            else S=(S==0?S:S-1);
            if(S==C)
            {
                printf("Controller %d\n",i+1);
                return ;
            }
            C=(C+dx+n)%n;
            if(C==0||C==n-1)dx=-dx;
            if(S==C)
            {
                printf("Controller %d\n",i+1);
                return ;
            }
        }
    }
    printf("Stowaway\n");
}
int main()
{
    while(~scanf("%d%d%d",&n,&S,&C))
    {
        getchar();
        gets(c);
        if(strcmp(c,"to head")==0)dx=-1;
        else dx=1;
        scanf("%s",s);
        Solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/v5zsq/article/details/81042632
74