题意:一只青蛙要从河的左岸到右岸找东西,找完东西要返回,河中间有呈直线的踮脚石,有两种石头,一种大石头,可以多次踩踏不沉,一种小石头,只能踩一次。
解题思路:
青蛙往返可以看作两只青蛙一起过河,求青蛙最大跨度
#include <cstdio>
#include <iostream>
#include <vector>
//#define T
using namespace std;
struct jump
{
int cnt;
int dis;
jump(char ch = ' ',int diss = 0)
{
if(ch == 'B')
cnt = 110;
else
cnt = 1;
dis = diss;
}
};
vector<jump> stone;
int main()
{
#ifdef T
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // T
int n;
scanf("%d",&n);
for(int cas = 1; cas <= n; cas ++)
{
stone.clear();
int a,b;
char ch1,ch2;
int d;
scanf("%d %d",&a,&b);
stone.push_back(jump('B',0));
for(int i = 0;i<a;i++)
{
scanf(" %c %c %d",&ch1,&ch2,&d); //注意前面加空格,以防读入char型换行符
// prlongf("ch1 = %c ch2 = %c d = %d\n",ch1,ch2,d);
stone.push_back(jump(ch1,d));
}
stone.push_back(jump('B',b));
int ans = 0;
int pre1 = 0;
int pre2 = 0;
for(int i = 0; i < stone.size(); i++)
{
// prlongf("stone.cnt --- %d stone.dis ---- %d i = %d\n",stone[i].cnt,stone[i].dis,i);
if(stone[i].cnt > 1)
{
if(stone[i].dis - pre1 >ans)
{
ans = stone[i].dis - pre1;
}
if(stone[i].dis - pre2 > ans) // 顺序别弄错
{
ans = stone[i].dis - pre2;
}
pre1 = stone[i].dis;
pre2 = stone[i].dis;
}
else
{
if(pre2 > pre1)
{
if(stone[i].dis - pre1 > ans)
ans = stone[i].dis - pre1;
pre1 = stone[i].dis;
}
else
{
if(stone[i].dis - pre2 > ans)
ans = stone[i].dis - pre2;
pre2 = stone[i].dis;
}
}
}
printf("Case %d: ",cas);
printf("%d\n",ans);
}
return 0;
}